Passed
Pull Request — master (#2506)
by Alan
03:41
created

EventDispatcher::getInternalEventData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\EventSubscriber;
15
16
use ApiPlatform\Core\Event\DeserializeEvent;
17
use ApiPlatform\Core\Event\PostDeserializeEvent;
18
use ApiPlatform\Core\Event\PostReadEvent;
19
use ApiPlatform\Core\Event\PreDeserializeEvent;
20
use ApiPlatform\Core\Event\PreReadEvent;
21
use ApiPlatform\Core\Event\ReadEvent;
22
use ApiPlatform\Core\Events;
23
use Symfony\Component\EventDispatcher\Event;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
27
use Symfony\Component\HttpKernel\KernelEvents;
28
29
/**
30
 * @internal
31
 *
32
 * @author Alan Poulain <[email protected]>
33
 */
34
final class EventDispatcher implements EventSubscriberInterface
35
{
36
    private const INTERNAL_EVENTS_CONFIGURATION = [
37
        KernelEvents::REQUEST => [
38
            PreReadEvent::class => Events::PRE_READ,
39
            ReadEvent::class => Events::READ,
40
            PostReadEvent::class => Events::POST_READ,
41
42
            PreDeserializeEvent::class => Events::PRE_DESERIALIZE,
43
            DeserializeEvent::class => Events::DESERIALIZE,
44
            PostDeserializeEvent::class => Events::POST_DESERIALIZE,
45
        ],
46
    ];
47
48
    private $dispatcher;
49
50
    public function __construct(EventDispatcherInterface $dispatcher)
51
    {
52
        $this->dispatcher = $dispatcher;
53
    }
54
55
    public static function getSubscribedEvents(): array
56
    {
57
        return [
58
            KernelEvents::REQUEST => 'dispatch',
59
        ];
60
    }
61
62
    public function dispatch(Event $event, string $eventName): void
63
    {
64
        $internalEventData = null;
65
        $internalEventContext = [];
66
67
        switch (true) {
68
            case $event instanceof GetResponseEvent:
69
                $internalEventContext = ['request' => $event->getRequest()];
70
        }
71
72
        foreach ($this->getInternalEventData($eventName) as $internalEventClass => $internalEventName) {
73
            $internalEvent = new $internalEventClass($internalEventData, $internalEventContext);
74
75
            $this->dispatcher->dispatch($internalEventName, $internalEvent);
0 ignored issues
show
Bug introduced by
$internalEventName of type array is incompatible with the type string expected by parameter $eventName of Symfony\Component\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            $this->dispatcher->dispatch(/** @scrutinizer ignore-type */ $internalEventName, $internalEvent);
Loading history...
76
        }
77
    }
78
79
    private function getInternalEventData(string $eventName): \Generator
80
    {
81
        yield from self::INTERNAL_EVENTS_CONFIGURATION[$eventName];
82
    }
83
}
84