-
Notifications
You must be signed in to change notification settings - Fork 0
/
BannerZoneSynchronizer.php
123 lines (106 loc) · 3.21 KB
/
BannerZoneSynchronizer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* This file is part of the Tadcka package.
*
* (c) Tadas Gliaubicas <tadcka89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silvestra\Component\Banner;
use Silvestra\Component\Banner\Event\BannerZoneEvent;
use Silvestra\Component\Banner\Event\BannerZoneEvents;
use Silvestra\Component\Banner\Model\BannerZoneInterface;
use Silvestra\Component\Banner\Model\Manager\BannerZoneManagerInterface;
use Silvestra\Component\Banner\Registry\BannerZoneConfig;
use Silvestra\Component\Banner\Registry\BannerZoneRegistry;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
/**
* @author Tadas Gliaubicas <tadcka89@gmail.com>
*
* @since 3/31/15 11:12 PM
*/
class BannerZoneSynchronizer
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var BannerZoneManagerInterface
*/
private $manager;
/**
* @var BannerZoneRegistry
*/
private $registry;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* Constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param BannerZoneManagerInterface $manager
* @param BannerZoneRegistry $registry
* @param TranslatorInterface $translator
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
BannerZoneManagerInterface $manager,
BannerZoneRegistry $registry,
TranslatorInterface $translator
) {
$this->eventDispatcher = $eventDispatcher;
$this->manager = $manager;
$this->registry = $registry;
$this->translator = $translator;
}
/**
* Synchronize system banner zones.
*
* @param string $locale
*/
public function synchronize($locale)
{
$existingSystemZones = $this->manager->findSystemSlugs();
$newZones = array();
foreach ($this->registry->getConfigs() as $config) {
if (false === in_array($config->getSlug(), $existingSystemZones)) {
$newZones[] = $this->createZone($config);
}
}
if (0 < count($newZones)) {
$this->manager->save();
foreach ($newZones as $zone) {
$this->eventDispatcher->dispatch(BannerZoneEvents::CREATE, new BannerZoneEvent($zone, $locale));
}
}
}
/**
* Create banner zone.
*
* @param BannerZoneConfig $config
*
* @return BannerZoneInterface
*/
private function createZone(BannerZoneConfig $config)
{
list($width, $height) = $config->getSize();
$zone = $this->manager->create();
$name = $config->getName();
if ($config->getTranslationDomain()) {
$name = $this->translator->trans($name, array(), $config->getTranslationDomain());
}
$zone->setName($name);
$zone->setCode($config->getSlug());
$zone->setSlug($config->getSlug());
$zone->setWidth($width);
$zone->setHeight($height);
$zone->setSystem(true);
$this->manager->add($zone);
return $zone;
}
}