Completed
Push — master ( e221c0...5a5b2e )
by Matze
05:22
created

EventCompilerPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 9
c 7
b 1
f 1
lcom 1
cbo 0
dl 0
loc 63
ccs 22
cts 25
cp 0.88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
A getEvents() 0 13 3
B handleEvent() 0 26 5
1
<?php
2
3
namespace BrainExe\Core\DependencyInjection\CompilerPass;
4
5
use BrainExe\Core\Annotations\CompilerPass;
6
use BrainExe\Core\EventDispatcher\AbstractEvent;
7
use BrainExe\Core\Traits\FileCacheTrait;
8
use Exception;
9
use ReflectionClass;
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
13
/**
14
 * @todo add event annotation
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
15
 * @CompilerPass
16
 */
17
class EventCompilerPass implements CompilerPassInterface
18
{
19
    use FileCacheTrait;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function process(ContainerBuilder $container)
25
    {
26 2
        $this->dumpVariableToCache('events', $this->getEvents());
27 2
    }
28
29
    /**
30
     * @return array
31
     * @throws Exception
32
     */
33 2
    private function getEvents()
34
    {
35 2
        $events = [];
36 2
        foreach (get_declared_classes() as $class) {
37 2
            $reflection = new ReflectionClass($class);
38
39 2
            if ($reflection->isSubclassOf(AbstractEvent::class)) {
40 2
                $this->handleEvent($reflection, $events, $class);
41
            }
42
        }
43
44 2
        return $events;
45
    }
46
47
    /**
48
     * @param ReflectionClass $reflection
49
     * @param $events
50
     * @param string $class
51
     * @throws Exception
52
     */
53 2
    private function handleEvent(ReflectionClass $reflection, array &$events, string $class)
54
    {
55 2
        foreach (array_values($reflection->getConstants()) as $constant) {
56 2
            if (strlen($constant) < 2) {
57 2
                continue;
58
            }
59 2
            if (isset($events[$constant])) {
60
                throw new Exception(sprintf(
61
                    'Event "%s" was already defined in "%s". (%s)',
62
                    $constant,
63
                    $events[$constant],
64
                    $class
65
                ));
66
            }
67
68 2
            $parameters = [];
69 2
            foreach ($reflection->getConstructor()->getParameters() as $parameter) {
70 2
                $parameters[] = $parameter->getName();
71
            }
72
73 2
            $events[$constant] = [
74 2
                'class'      => $class,
75 2
                'parameters' => $parameters
76
            ];
77
        }
78 2
    }
79
}
80