Completed
Push — demo-bundle ( 579cdc...8a40b3 )
by Quentin
03:24
created

CreateCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 95
rs 10
c 0
b 0
f 0
ccs 22
cts 32
cp 0.6875

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
createTemplate() 0 1 ?
A onTemplateCreated() 0 11 3
A resolve() 0 19 1
A setTemplateType() 0 6 1
1
<?php
2
3
namespace Synapse\Cmf\Framework\Theme\Template\Domain\Command;
4
5
use Synapse\Cmf\Framework\Theme\TemplateType\Model\TemplateTypeInterface;
6
use Synapse\Cmf\Framework\Theme\Template\Entity\Template;
7
use Synapse\Cmf\Framework\Theme\Template\Event\Event as TemplateEvent;
8
use Synapse\Cmf\Framework\Theme\Template\Event\Events as TemplateEvents;
9
use Synapse\Cmf\Framework\Theme\Zone\Domain\DomainInterface as ZoneDomain;
10
use Synapse\Cmf\Framework\Theme\Zone\Entity\ZoneCollection;
11
12
/**
13
 * Template creation command representation.
14
 */
15
abstract class CreateCommand extends AbstractCommand
16
{
17
    /**
18
     * @var string
19
     */
20
    private $templateClass;
21
22
    /**
23
     * @var ZoneDomain
24
     */
25
    private $zoneDomain;
26
27
    /**
28
     * @var TemplateTypeInterface
29
     */
30
    protected $templateType;
31
32
    /**
33
     * Construct.
34
     *
35
     * @param ZoneDomain $zoneDomain
36
     * @param string     $templateClass
37
     */
38 2
    public function __construct(ZoneDomain $zoneDomain, $templateClass = Template::class)
39
    {
40 2
        $this->zoneDomain = $zoneDomain;
41 2
        $this->templateClass = $templateClass;
42 2
        $this->zones = new ZoneCollection();
43 2
    }
44
45
    /**
46
     * Template object creation method.
47
     *
48
     * @param string $templateClass
49
     *
50
     * @return TemplateInterface
51
     */
52
    abstract protected function createTemplate($templateClass);
53
54
    /**
55
     * Template creation event handler.
56
     *
57
     * @param TemplateEvent $event
58
     */
59
    public function onTemplateCreated(TemplateEvent $event)
60
    {
61
        $template = $event->getTemplate();
62
        $zones = $template->getZones();
63
        foreach ($template->getTemplateType()->getZoneTypes() as $zoneType) {
64
            if ($zones->search(array('zoneType' => $zoneType))->isEmpty()) {
65
                $zones->add($this->zoneDomain->create($zoneType));
66
            }
67
        }
68
        $template->setZones($zones);
69
    }
70
71
    /**
72
     * Template creation method.
73
     *
74
     * @return Template
75
     */
76 2
    public function resolve()
77
    {
78 2
        $this->template = $this->createTemplate($this->templateClass);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createTemplate($this->templateClass) of type object<Synapse\Cmf\Frame...mand\TemplateInterface> is incompatible with the declared type object<Synapse\Cmf\Frame...odel\TemplateInterface> of property $template.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79 2
        $this->template->setZones($this->zones);
80
81 2
        $this->assertEntityIsValid($this->template, array('Template', 'creation'));
82
83 2
        $this->eventDispatcher->addListener(
84 2
            TemplateEvents::TEMPLATE_CREATED,
85 2
            $handler = array($this, 'onTemplateCreated')
86 1
        );
87 2
        $this->fireEvent(
88 2
            TemplateEvents::TEMPLATE_CREATED,
89 2
            new TemplateEvent($this->template, $this)
90 1
        );
91 2
        $this->eventDispatcher->removeListener(TemplateEvents::TEMPLATE_CREATED, $handler);
92
93 2
        return $this->template;
94
    }
95
96
    /**
97
     * Define action template type.
98
     *
99
     * @param TemplateTypeInterface $templateType
100
     *
101
     * @return self
102
     */
103 2
    public function setTemplateType(TemplateTypeInterface $templateType)
104
    {
105 2
        $this->templateType = $templateType;
106
107 2
        return $this;
108
    }
109
}
110