-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composite.php
67 lines (61 loc) · 2.03 KB
/
Composite.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
<?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\Composite\ElementInterface;
use ArtemiiKarkusha\DesignPatterns\Model\Composite\LeafFactory;
use ArtemiiKarkusha\DesignPatterns\Model\Composite\NodeFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
/**
* Controller for test Builder functionality
*/
class Composite implements HttpGetActionInterface
{
/**
* @param LeafFactory $leafFactory
* @param NodeFactory $nodeFactory
* @param ResultFactory $resultFactory
*/
public function __construct(
private LeafFactory $leafFactory,
private NodeFactory $nodeFactory,
private ResultFactory $resultFactory
) {
}
/**
* @inheritDoc
*/
public function execute(): ResultInterface
{
return $this->resultFactory->create(ResultFactory::TYPE_RAW)
->setContents(sprintf('Number for tree: %s. It must be 11.', $this->getTree()->getNumber()));
}
/**
* @return ElementInterface
*/
private function getTree(): ElementInterface
{
/** @var ElementInterface $tree */
$tree = $this->nodeFactory->create();
$branch1 = $this->nodeFactory->create();
$branch1->add($this->leafFactory->create());
$branch1->add($this->leafFactory->create());
$branch1->add($this->leafFactory->create());
$branch1->decrement();
$branch2 = $this->nodeFactory->create();
$branch2->add($this->leafFactory->create());
$branch2->add($this->leafFactory->create());
$branch2->add($this->leafFactory->create());
$branch2->add($this->leafFactory->create());
$tree->add($branch1);
$tree->add($branch2);
$tree->increment();
$tree->increment();
return $tree;
}
}