Completed
Push — master ( 28dda4...aabe55 )
by Craig
06:13
created

HookDispatcher::reload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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\HookBundle\Collector\HookCollectorInterface;
17
use Zikula\Bundle\HookBundle\Dispatcher\Exception\LogicException;
18
use Zikula\Bundle\HookBundle\Hook\Hook;
19
20
/**
21
 * HookDispatcher class.
22
 */
23
class HookDispatcher implements HookDispatcherInterface
24
{
25
    /**
26
     * Storage.
27
     *
28
     * @var StorageInterface
29
     */
30
    private $storage;
31
32
    /**
33
     * @var HookCollectorInterface
34
     */
35
    private $hookCollector;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $dispatcher;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param StorageInterface $storage
46
     * @param HookCollectorInterface $hookCollector
47
     * @param EventDispatcherInterface $dispatcher
48
     */
49
    public function __construct(
50
        StorageInterface $storage,
51
        HookCollectorInterface $hookCollector,
52
        EventDispatcherInterface $dispatcher
53
    ) {
54
        $this->storage = $storage;
55
        $this->hookCollector = $hookCollector;
56
        $this->dispatcher = $dispatcher;
57
    }
58
59
    /**
60
     * Get storage driver.
61
     *
62
     * @return StorageInterface
63
     */
64
    public function getStorage()
65
    {
66
        return $this->storage;
67
    }
68
69
    /**
70
     * Dispatch hook listeners.
71
     *
72
     * @param string $name Hook event name
73
     * @param Hook   $hook Hook instance
74
     *
75
     * @return Event
76
     */
77
    public function dispatch($name, Hook $hook)
78
    {
79
        $this->decorateHook($name, $hook);
80
        if (!$hook->getAreaId()) {
81
            return $hook;
82
        }
83
84
        return $this->dispatcher->dispatch($name, $hook);
85
    }
86
87
    /**
88
     * Return all bindings for a given area.
89
     *
90
     * Area names are unique so you can specify subscriber or provider area.
91
     *
92
     * @param string $areaName Areaname
93
     * @param string $type subscriber|provider
94
     *
95
     * @return array
96
     */
97
    public function getBindingsFor($areaName, $type = 'subscriber')
98
    {
99
        return $this->storage->getBindingsFor($areaName, $type);
100
    }
101
102
    /**
103
     * Set the bind order of hooks.
104
     *
105
     * Used to resort the order providers are invoked for a given
106
     * area name.
107
     *
108
     * @param string $subscriberAreaName
109
     * @param array  $providerAreas      Array of provider area names
110
     */
111
    public function setBindOrder($subscriberAreaName, array $providerAreas)
112
    {
113
        $this->storage->setBindOrder($subscriberAreaName, $providerAreas);
114
    }
115
116
    /**
117
     * Get binding between areas.
118
     *
119
     * @param string $subscriberArea
120
     * @param string $providerArea
121
     *
122
     * @return array
123
     */
124
    public function getBindingBetweenAreas($subscriberArea, $providerArea)
125
    {
126
        return $this->storage->getBindingBetweenAreas($subscriberArea, $providerArea);
127
    }
128
129
    /**
130
     * Check if areas may be bound together.
131
     *
132
     * @param string $subscriberarea
133
     * @param string $providerarea
134
     *
135
     * @return boolean
136
     */
137
    public function isAllowedBindingBetweenAreas($subscriberarea, $providerarea)
138
    {
139
        return $this->storage->isAllowedBindingBetweenAreas($subscriberarea, $providerarea);
140
    }
141
142
    /**
143
     * Get bindings between two owners.
144
     *
145
     * @param string $subscriberName
146
     * @param string $providerName
147
     *
148
     * @return array
149
     */
150
    public function getBindingsBetweenOwners($subscriberName, $providerName)
151
    {
152
        return $this->storage->getBindingsBetweenOwners($subscriberName, $providerName);
153
    }
154
155
    /**
156
     * Bind subscriber and provider area together.
157
     *
158
     * @param string $subscriberArea
159
     * @param string $providerArea
160
     *
161
     * @throws LogicException
162
     */
163
    public function bindSubscriber($subscriberArea, $providerArea)
164
    {
165
        $this->storage->bindSubscriber($subscriberArea, $providerArea);
166
    }
167
168
    /**
169
     * Unbind subscriber.
170
     *
171
     * @param string $subscriberArea
172
     * @param string $providerArea
173
     */
174
    public function unbindSubscriber($subscriberArea, $providerArea)
175
    {
176
        return $this->storage->unbindSubscriber($subscriberArea, $providerArea);
177
    }
178
179
    /**
180
     * Decorate hook with required metadata.
181
     *
182
     * @param $name
183
     * @param Hook $hook
184
     */
185
    private function decorateHook($name, Hook $hook)
186
    {
187
        $owningSide = $this->storage->getRuntimeMetaByEventName($name);
188
        if ($owningSide) {
189
            $hook->setAreaId($owningSide['areaid']);
190
            if (!$hook->getCaller()) {
191
                $hook->setCaller($owningSide['owner']);
192
            }
193
        }
194
    }
195
}
196