-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singleton.php
71 lines (62 loc) · 1.84 KB
/
Singleton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* @author Artemii Karkusha
* @copyright Copyright (c) (https://www.linkedin.com/in/artemiy-karkusha/)
*/
declare(strict_types=1);
namespace ArtemiiKarkusha\DesignPatterns\Controller\Test;
use InvalidArgumentException;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Controller\ResultFactory;
use ArtemiiKarkusha\DesignPatterns\Service\Singleton\Persons;
/**
* Controller for test Singleton functionality
*/
class Singleton implements HttpGetActionInterface
{
/**
* @var ResultFactory
*/
private ResultFactory $resultFactory;
/**
* @param ResultFactory $resultFactory
*/
public function __construct(
ResultFactory $resultFactory
) {
$this->resultFactory = $resultFactory;
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
try {
$persons = [];
for ($i = 0; $i < 15; $i++) {
$persons[] = Persons::getInstance();
}
} catch (InvalidArgumentException $invalidArgumentException) {
throw new NotFoundException(__($invalidArgumentException->getMessage()));
}
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($this->getResponseText($persons));
}
/**
* @param Persons[] $personsList
* @return string
*/
private function getResponseText(array $personsList): string
{
$responseText = '';
foreach ($personsList as $persons) {
$responseText .= sprintf(
'<br>Persons data number"#%s"{ persons hash: "%s"}',
spl_object_id($persons),
$persons->getHash(),
);
}
return $responseText;
}
}