Completed
Pull Request — master (#4522)
by Craig
13:45
created

EventDispatcherRuntime::dispatchEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle\Twig\Runtime;
15
16
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
17
use Twig\Extension\RuntimeExtensionInterface;
18
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
19
20
class EventDispatcherRuntime implements RuntimeExtensionInterface
21
{
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $eventDispatcher;
26
27
    public function __construct(
28
        EventDispatcherInterface $eventDispatcher
29
    ) {
30
        $this->eventDispatcher = $eventDispatcher;
31
    }
32
33
    /**
34
     * @param array $arguments the arguments, MUST be values only and in the correct order
35
     */
36
    public function dispatchEvent(string $name, $arguments = [])
37
    {
38
        if (class_exists($name)) {
39
            $event = new $name(...$arguments);
40
            $this->eventDispatcher->dispatch($event);
41
42
            return $event;
43
        }
44
45
        return null;
46
    }
47
48
    public function dispatchGenericEvent(string $name, GenericEvent $providedEvent = null, $subject = null, array $arguments = [], $data = null)
49
    {
50
        $event = $providedEvent ?? new GenericEvent($subject, $arguments, $data);
51
        $this->eventDispatcher->dispatch($event, $name);
52
53
        return $event->getData();
54
    }
55
}
56