Completed
Pull Request — master (#10)
by Matze
07:31
created

EventCompilerPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 70.37%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 46
ccs 19
cts 27
cp 0.7037
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 38 7
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
                $events[$constant] = [
53 2
                    'class'      => $class,
54 2
                    'parameters' => $reflection->getConstructor()->getNumberOfParameters()
55 2
                ];
56 2
            };
57 2
        }
58
59 2
        $this->dumpVariableToCache('events', $events);
60 2
    }
61
}
62