-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visitor.php
76 lines (67 loc) · 2.31 KB
/
Visitor.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
72
73
74
75
76
<?php
/**
* @author Artemii Karkusha
* @copyright Copyright (c) (https://www.linkedin.com/in/artemiy-karkusha/)
*/
declare(strict_types=1);
namespace ArtemiiKarkusha\DesignPatterns\Controller\Test;
use ArtemiiKarkusha\DesignPatterns\Api\Builder\PizzaInterface;
use ArtemiiKarkusha\DesignPatterns\Model\Builder\Bacon;
use ArtemiiKarkusha\DesignPatterns\Model\Builder\Cheese;
use ArtemiiKarkusha\DesignPatterns\Model\Builder\Pineapples;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use ArtemiiKarkusha\DesignPatterns\Api\Visitor\VisitorInterface;
use ArtemiiKarkusha\DesignPatterns\Api\Builder\PizzaBuilderInterface;
use Magento\Framework\Controller\ResultInterface;
class Visitor implements HttpGetActionInterface
{
/**
* @param ResultFactory $resultFactory
* @param VisitorInterface $visitorPizzaMaker
* @param PizzaBuilderInterface $pizzaBuilder
*/
public function __construct(
readonly private ResultFactory $resultFactory,
readonly private VisitorInterface $visitorPizzaMaker,
readonly private PizzaBuilderInterface $pizzaBuilder
) {
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($this->getContents());
}
/**
* @return string
*/
public function getContents(): string
{
$this->visitorPizzaMaker->visitPineapple(new Pineapples());
$this->visitorPizzaMaker->visitCheese(new Cheese());
$this->visitorPizzaMaker->visitBacon(new Bacon());
$this->visitorPizzaMaker->visitBacon(new Bacon());
return $this->getResponseText($this->pizzaBuilder->getPizza());
}
/**
* @param PizzaInterface $pizza
* @return string
*/
private function getResponseText(PizzaInterface $pizza): string
{
if (count($pizza->getIngredients()) === 0) {
return '';
}
$responseText = sprintf("<br>Pizza number\"#%s\"{", spl_object_id($pizza));
foreach ($pizza->getIngredients() as $ingredient) {
$responseText .= sprintf(
'ingredient: "%s", ',
$ingredient->getName(),
);
}
$responseText .= '}';
return $responseText;
}
}