-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
73 lines (66 loc) · 2.17 KB
/
Command.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
<?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\Api\Command\Service\PizzaCookerInterface;
use ArtemiiKarkusha\DesignPatterns\Model\Builder\Bacon;
use ArtemiiKarkusha\DesignPatterns\Model\Builder\Cheese;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NotFoundException;
class Command implements HttpGetActionInterface
{
/**
* @param ResultFactory $resultFactory
* @param PizzaCookerInterface $pizzaCooker
*/
public function __construct(
private ResultFactory $resultFactory,
private PizzaCookerInterface $pizzaCooker
) {
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
return $this->resultFactory->create(ResultFactory::TYPE_RAW)
->setContents($this->getContents());
}
/**
* @return string
*/
public function getContents(): string
{
try {
$pizzaWithBaconAndCheese = $this->pizzaCooker
->addIngredientByName(Bacon::INGREDIENT_NAME)
->addIngredientByName(Cheese::INGREDIENT_NAME)
->makePizza();
return sprintf(
'Pizza ingredients: %s. Pizza objectId : %s',
$this->convertPizzaIngratesToString($pizzaWithBaconAndCheese),
spl_object_id($pizzaWithBaconAndCheese),
);
} catch (NotFoundException $exception) {
return $exception->getMessage();
}
}
/**
* @param PizzaInterface $pizza
* @return string
*/
private function convertPizzaIngratesToString(PizzaInterface $pizza): string
{
$ingredientsHowString = '';
foreach ($pizza->getIngredients() as $ingredient) {
$ingredientsHowString .= $ingredient->getName() . ',';
}
return $ingredientsHowString;
}
}