Completed
Push — master ( c4b22f...ba9072 )
by Matze
08:46
created

EventCompilerPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 82.61%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 8
c 4
b 1
f 0
lcom 0
cbo 0
dl 0
loc 51
ccs 19
cts 23
cp 0.8261
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 43 8
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
        if (!is_dir(ROOT . 'cache')) {
26
            return;
27
        }
28
29 2
        $events = [];
30 2
        foreach (get_declared_classes() as $class) {
31 2
            $reflection = new ReflectionClass($class);
32
33 2
            if (!$reflection->isSubclassOf(AbstractEvent::class)) {
34 2
                continue;
35
            }
36
37 2
            foreach (array_values($reflection->getConstants()) as $constant) {
38 2
                if (strlen($constant) < 2) {
39 2
                    continue;
40
                }
41 2
                if (isset($events[$constant])) {
42
                    throw new Exception(
43
                        sprintf(
44
                            'Event "%s" was already defined in "%s". (%s)',
45
                            $constant,
46
                            $events[$constant],
47
                            $class
48
                        )
49
                    );
50
                }
51
52 2
                $parameters = [];
53 2
                foreach ($reflection->getConstructor()->getParameters() as $parameter) {
54 2
                    $parameters[] = $parameter->getName();
55
                }
56
57 2
                $events[$constant] = [
58 2
                    'class'      => $class,
59 2
                    'parameters' => $parameters
60
                ];
61
            };
62
        }
63
64 2
        $this->dumpVariableToCache('events', $events);
65 2
    }
66
}
67