-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.php
More file actions
60 lines (53 loc) · 1.74 KB
/
FactoryMethod.php
File metadata and controls
60 lines (53 loc) · 1.74 KB
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
<?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\Model\FactoryMethod\Meat;
use ArtemiiKarkusha\DesignPatterns\Model\FactoryMethod\Potato;
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\FactoryMethod\Bake;
/**
* Controller for test FactoryMethod functionality
*/
class FactoryMethod implements HttpGetActionInterface
{
/**
* @var ResultFactory
*/
private ResultFactory $resultFactory;
/**
* @var Bake
*/
private Bake $bake;
/**
* @param ResultFactory $resultFactory
* @param Bake $bake
*/
public function __construct(ResultFactory $resultFactory, Bake $bake)
{
$this->resultFactory = $resultFactory;
$this->bake = $bake;
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
try {
$meat = $this->bake->cook(Meat::MEAT_NAME);
$potato = $this->bake->cook(Potato::MEAT_NAME);
$responseText = sprintf('Meat getName is %s', $meat->getName());
$responseText .= sprintf(', Potato getName is %s', $potato->getName());
} catch (InvalidArgumentException $invalidArgumentException) {
throw new NotFoundException(__($invalidArgumentException->getMessage()));
}
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($responseText);
}
}