-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractFactory.php
80 lines (70 loc) · 2.48 KB
/
AbstractFactory.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
77
78
79
80
<?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 ArtemiiKarkusha\DesignPatterns\Api\FactoryMethod\FoodInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Controller\ResultFactory;
use ArtemiiKarkusha\DesignPatterns\Service\AbstractFactory\KitchenAbstractFactory;
/**
* Controller for test AbstractFactory functionality
*/
class AbstractFactory implements HttpGetActionInterface
{
public const KITCHEN_LIST = ['ukrainian_kitchen', 'american_kitchen', 'japanese_kitchen'];
/**
* @var ResultFactory
*/
private ResultFactory $resultFactory;
/**
* @var KitchenAbstractFactory
*/
private KitchenAbstractFactory $kitchenAbstractFactory;
/**
* @param ResultFactory $resultFactory
* @param KitchenAbstractFactory $kitchenAbstractFactory
*/
public function __construct(ResultFactory $resultFactory, KitchenAbstractFactory $kitchenAbstractFactory)
{
$this->resultFactory = $resultFactory;
$this->kitchenAbstractFactory = $kitchenAbstractFactory;
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
$foods = [];
try {
foreach (self::KITCHEN_LIST as $kitchenName) {
$foods[$kitchenName] = $this->kitchenAbstractFactory->creatFoods($kitchenName);
}
} catch (InvalidArgumentException $invalidArgumentException) {
throw new NotFoundException(__($invalidArgumentException->getMessage()));
}
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($this->getResponseText($foods));
}
/**
* @param FoodInterface[] $foods
* @return string
*/
private function getResponseText(array $foods): string
{
$responseText = '';
foreach ($foods as $kitchenName => $foodsFromKitchenFactory) {
$responseText .= '<br>Kithcen name: "' . $kitchenName . '" products from kitchen';
/** @var FoodInterface $food */
foreach ($foodsFromKitchenFactory as $food) {
$responseText .= ', ' . $food->getName();
}
}
return $responseText;
}
}