Completed
Push — update-template-zone ( 373085 )
by
unknown
16:30
created

AbstractCommand::onTemplateCreated()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Synapse\Cmf\Framework\Theme\Template\Domain\Command;
4
5
use Majora\Framework\Domain\Action\ActionInterface;
6
use Majora\Framework\Domain\Action\Dal\DalActionTrait;
7
use Synapse\Cmf\Framework\Theme\Template\Entity\Template;
8
use Synapse\Cmf\Framework\Theme\Template\Model\TemplateInterface;
9
use Synapse\Cmf\Framework\Theme\Zone\Entity\ZoneCollection;
10
use Synapse\Cmf\Framework\Theme\Zone\Domain\DomainInterface as ZoneDomain;
11
12
/**
13
 * Base class for Template commands.
14
 */
15
abstract class AbstractCommand implements ActionInterface
16
{
17
    use DalActionTrait;
18
19
    /**
20
     * @var TemplateInterface
21
     */
22
    protected $template;
23
24
    /**
25
     * @var ZoneCollection
26
     */
27
    protected $zones;
28
29
    /**
30
     * @var ZoneDomain
31
     */
32
    protected $zoneDomain;
33
34
    /**
35
     * Template creation event handler.
36
     *
37
     * @param TemplateEvent $event
38
     */
39
    public function onTemplateCreated(TemplateEvent $event)
40
    {
41
        $template = $event->getTemplate();
42
        $zones = $template->getZones();
43
        foreach ($template->getTemplateType()->getZoneTypes() as $zoneType) {
44
            if ($zones->search(array('zoneType' => $zoneType))->isEmpty()) {
45
                if (null === $this->zoneDomain) {
46
                    throw new \DomainException('No zoneDomain was set, you are unable to update your configuration.');
47
                }
48
49
                $zones->add($this->zoneDomain->create($zoneType));
50
            }
51
        }
52
        $template->setZones($zones);
53
    }
54
55
    /**
56
     * Return related Template if defined.
57
     *
58
     * @return TemplateInterface $template
59
     */
60
    public function getTemplate()
61
    {
62
        return $this->template;
63
    }
64
65
    /**
66
     * Define Command zones.
67
     *
68
     * @param ZoneCollection $zones
69
     *
70
     * @return self
71
     */
72
    public function setZones(ZoneCollection $zones)
73
    {
74
        $this->zones = $zones;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Returns Command zones.
81
     *
82
     * @return ZoneCollection
83
     */
84
    public function getZones()
85
    {
86
        return $this->zones;
87
    }
88
89
    /**
90
     * Set zoneDomain.
91
     *
92
     * @param ZoneDomain $zoneDomain
93
     */
94
    public function setZoneDomain(ZoneDomain $zoneDomain)
95
    {
96
        $this->zoneDomain = $zoneDomain;
97
    }
98
}
99