Completed
Push — master ( b1744c...51aee2 )
by Matze
04:34
created

EventCompilerPass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 75
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
A getEvents() 0 13 3
B handleEvent() 0 24 5
A getParameters() 0 9 2
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
 * @CompilerPass
15
 */
16
class EventCompilerPass implements CompilerPassInterface
17
{
18
    use FileCacheTrait;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 2
    public function process(ContainerBuilder $container)
24
    {
25 2
        $this->dumpVariableToCache('events', $this->getEvents());
26 2
    }
27
28
    /**
29
     * @return array
30
     * @throws Exception
31
     */
32 2
    private function getEvents()
33
    {
34 2
        $events = [];
35 2
        foreach (get_declared_classes() as $class) {
36 2
            $reflection = new ReflectionClass($class);
37
38 2
            if ($reflection->isSubclassOf(AbstractEvent::class)) {
39 2
                $this->handleEvent($reflection, $events, $class);
40
            }
41
        }
42
43 2
        return $events;
44
    }
45
46
    /**
47
     * @param ReflectionClass $reflection
48
     * @param $events
49
     * @param string $class
50
     * @throws Exception
51
     */
52 2
    private function handleEvent(ReflectionClass $reflection, array &$events, string $class)
53
    {
54 2
        foreach (array_values($reflection->getConstants()) as $constant) {
55 2
            if (strlen($constant) < 3 || strpos($constant, '.') === false) {
56 2
                continue;
57
            }
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 = $this->getParameters($reflection);
69
70 2
            $events[$constant] = [
71 2
                'class'      => $class,
72 2
                'parameters' => $parameters
73
            ];
74
        }
75 2
    }
76
77
    /**
78
     * @param ReflectionClass $reflection
79
     * @return array
80
     */
81 2
    private function getParameters(ReflectionClass $reflection)
82
    {
83 2
        $parameters = [];
84 2
        foreach ($reflection->getConstructor()->getParameters() as $parameter) {
85 2
            $parameters[] = $parameter->getName();
86
        }
87
88 2
        return $parameters;
89
    }
90
}
91