Completed
Push — master ( 1a6be8...e04a88 )
by Craig
09:51
created

HookDispatcher::loadRuntimeHandlers()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A HookDispatcher::reload() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\HookBundle\Dispatcher;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Zikula\Bundle\CoreBundle\CacheClearer;
17
use Zikula\Bundle\HookBundle\Bundle\SubscriberBundle;
18
use Zikula\Bundle\HookBundle\Bundle\ProviderBundle;
19
use Zikula\Bundle\HookBundle\Dispatcher\Exception\LogicException;
20
use Zikula\Bundle\HookBundle\Hook\Hook;
21
22
/**
23
 * HookDispatcher class.
24
 */
25
class HookDispatcher implements HookDispatcherInterface
26
{
27
    /**
28
     * Storage.
29
     *
30
     * @var StorageInterface
31
     */
32
    private $storage;
33
34
    /**
35
     * @var EventDispatcherInterface
36
     */
37
    private $dispatcher;
38
39
    /**
40
     * @var CacheClearer
41
     */
42
    private $cacheClearer;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param StorageInterface         $storage
48
     * @param EventDispatcherInterface $dispatcher
49
     */
50
    public function __construct(
51
        StorageInterface $storage,
52
        EventDispatcherInterface $dispatcher,
53
        CacheClearer $cacheClearer
54
    ) {
55
        $this->storage = $storage;
56
        $this->dispatcher = $dispatcher;
57
        $this->cacheClearer = $cacheClearer;
58
    }
59
60
    /**
61
     * Get storage driver.
62
     *
63
     * @return StorageInterface
64
     */
65
    public function getStorage()
66
    {
67
        return $this->storage;
68
    }
69
70
    /**
71
     * Dispatch hook listeners.
72
     *
73
     * @param string $name Hook event name
74
     * @param Hook   $hook Hook instance
75
     *
76
     * @return Event
77
     */
78
    public function dispatch($name, Hook $hook)
79
    {
80
        $this->decorateHook($name, $hook);
81
        if (!$hook->getAreaId()) {
82
            return $hook;
83
        }
84
85
        return $this->dispatcher->dispatch($name, $hook);
86
    }
87
88
    /**
89
     * Register a subscriber bundle with persistence.
90
     *
91
     * @param SubscriberBundle $bundle
92
     */
93
    public function registerSubscriberBundle(SubscriberBundle $bundle)
94
    {
95
        foreach ($bundle->getEvents() as $areaType => $eventName) {
96
            $this->storage->registerSubscriber($bundle->getOwner(), $bundle->getSubOwner(), $bundle->getArea(), $areaType, $bundle->getCategory(), $eventName);
97
        }
98
99
        $this->reload();
100
    }
101
102
    /**
103
     * Unregister a subscriber bundle from persistence.
104
     *
105
     * @param SubscriberBundle $bundle
106
     */
107
    public function unregisterSubscriberBundle(SubscriberBundle $bundle)
108
    {
109
        $this->storage->unregisterSubscriberByArea($bundle->getArea());
110
111
        $this->reload();
112
    }
113
114
    /**
115
     * Register provider bundle with persistence.
116
     *
117
     * @param ProviderBundle $bundle
118
     */
119
    public function registerProviderBundle(ProviderBundle $bundle)
120
    {
121
        foreach ($bundle->getHooks() as $hook) {
122
            $this->storage->registerProvider($bundle->getOwner(), $bundle->getSubOwner(), $bundle->getArea(), $hook['hooktype'], $bundle->getCategory(), $hook['classname'], $hook['method'], $hook['serviceid']);
123
        }
124
125
        $this->reload();
126
    }
127
128
    /**
129
     * Unregister a provider bundle with persistence.
130
     *
131
     * @param ProviderBundle $bundle
132
     */
133
    public function unregisterProviderBundle(ProviderBundle $bundle)
134
    {
135
        $this->storage->unregisterProviderByArea($bundle->getArea());
136
137
        $this->reload();
138
    }
139
140
    /**
141
     * Return all bindings for a given area.
142
     *
143
     * Area names are unique so you can specify subscriber or provider area.
144
     *
145
     * @param string $areaName Areaname
146
     *
147
     * @return array
148
     */
149
    public function getBindingsFor($areaName)
150
    {
151
        return $this->storage->getBindingsFor($areaName);
152
    }
153
154
    /**
155
     * Get subscriber areas for an owner.
156
     *
157
     * @param string $owner
158
     *
159
     * @return array
160
     */
161
    public function getSubscriberAreasByOwner($owner)
162
    {
163
        return $this->storage->getSubscriberAreasByOwner($owner);
164
    }
165
166
    /**
167
     * Get provider areas for an owner.
168
     *
169
     * @param string $owner
170
     *
171
     * @return array
172
     */
173
    public function getProviderAreasByOwner($owner)
174
    {
175
        return $this->storage->getProviderAreasByOwner($owner);
176
    }
177
178
    /**
179
     * Get owber by area.
180
     *
181
     * @param string $areaName
182
     *
183
     * @return string
184
     */
185
    public function getOwnerByArea($areaName)
186
    {
187
        return $this->storage->getOwnerByArea($areaName);
188
    }
189
190
    /**
191
     * Get area id.
192
     *
193
     * @param string $areaName
194
     *
195
     * @return integer
196
     */
197
    public function getAreaId($areaName)
198
    {
199
        return $this->storage->getAreaId($areaName);
200
    }
201
202
    /**
203
     * Set the bind order of hooks.
204
     *
205
     * Used to resort the order providers are invoked for a given
206
     * area name.
207
     *
208
     * @param string $subscriberAreaName
209
     * @param array  $providerAreas      Array of provider area names
210
     */
211
    public function setBindOrder($subscriberAreaName, array $providerAreas)
212
    {
213
        $this->storage->setBindOrder($subscriberAreaName, $providerAreas);
214
        $this->reload();
215
    }
216
217
    /**
218
     * Get binding between areas.
219
     *
220
     * @param string $subscriberArea
221
     * @param string $providerArea
222
     *
223
     * @return array
224
     */
225
    public function getBindingBetweenAreas($subscriberArea, $providerArea)
226
    {
227
        return $this->storage->getBindingBetweenAreas($subscriberArea, $providerArea);
228
    }
229
230
    /**
231
     * Check if areas may be bound together.
232
     *
233
     * @param string $subscriberarea
234
     * @param string $providerarea
235
     *
236
     * @return boolean
237
     */
238
    public function isAllowedBindingBetweenAreas($subscriberarea, $providerarea)
239
    {
240
        return $this->storage->isAllowedBindingBetweenAreas($subscriberarea, $providerarea);
241
    }
242
243
    /**
244
     * Get bindings between two owners.
245
     *
246
     * @param string $subscriberName
247
     * @param string $providerName
248
     *
249
     * @return array
250
     */
251
    public function getBindingsBetweenOwners($subscriberName, $providerName)
252
    {
253
        return $this->storage->getBindingsBetweenOwners($subscriberName, $providerName);
254
    }
255
256
    /**
257
     * Bind subscriber and provider area together.
258
     *
259
     * @param string $subscriberArea
260
     * @param string $providerArea
261
     *
262
     * @throws LogicException
263
     */
264
    public function bindSubscriber($subscriberArea, $providerArea)
265
    {
266
        $this->storage->bindSubscriber($subscriberArea, $providerArea);
267
    }
268
269
    /**
270
     * Unbind subscriber.
271
     *
272
     * @param string $subscriberArea
273
     * @param string $providerArea
274
     */
275
    public function unbindSubscriber($subscriberArea, $providerArea)
276
    {
277
        return $this->storage->unbindSubscriber($subscriberArea, $providerArea);
278
    }
279
280
    /**
281
     * Decorate hook with required metadata.
282
     *
283
     * @param $name
284
     * @param Hook $hook
285
     */
286
    private function decorateHook($name, Hook $hook)
287
    {
288
        $owningSide = $this->storage->getRuntimeMetaByEventName($name);
289
        if ($owningSide) {
290
            $hook->setAreaId($owningSide['areaid']);
291
            if (!$hook->getCaller()) {
292
                $hook->setCaller($owningSide['owner']);
293
            }
294
        }
295
    }
296
297
    private function reload()
298
    {
299
        // recompile the container
300
        $this->cacheClearer->clear('symfony.config');
301
    }
302
}
303