Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

EventDispatcherExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
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\CoreBundle\Twig\Extension;
13
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Zikula\Core\Event\GenericEvent;
16
17
class EventDispatcherExtension extends \Twig_Extension
18
{
19
    /**
20
     * @var EventDispatcherInterface
21
     */
22
    private $dispatcher;
23
24
    /**
25
     * EventDispatcherExtension constructor.
26
     * @param EventDispatcherInterface $eventDispatcher
27
     */
28
    public function __construct(
29
        EventDispatcherInterface $eventDispatcher
30
    ) {
31
        $this->dispatcher = $eventDispatcher;
32
    }
33
34
    /**
35
     * Returns a list of functions to add to the existing list.
36
     *
37
     * @return array An array of functions
38
     */
39
    public function getFunctions()
40
    {
41
        return [
42
            new \Twig_SimpleFunction('dispatchEvent', [$this, 'dispatchEvent']),
43
        ];
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param GenericEvent|null $providedEvent
49
     * @param null $subject
50
     * @param array $arguments
51
     * @param null $data
52
     * @return mixed
53
     */
54
    public function dispatchEvent($name, GenericEvent $providedEvent = null, $subject = null, array $arguments = [], $data = null)
55
    {
56
        $event = isset($providedEvent) ? $providedEvent : new GenericEvent($subject, $arguments, $data);
57
        $this->dispatcher->dispatch($name, $event);
58
59
        return $event->getData();
60
    }
61
}
62