LifecycleEvents::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 3
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use Doctrine\ORM\Events;
6
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
7
use LaravelDoctrine\Fluent\Buildable;
8
9
/**
10
 * @method LifecycleEvents preRemove(string $method)
11
 * @method LifecycleEvents postRemove(string $method)
12
 * @method LifecycleEvents prePersist(string $method)
13
 * @method LifecycleEvents postPersist(string $method)
14
 * @method LifecycleEvents preUpdate(string $method)
15
 * @method LifecycleEvents postUpdate(string $method)
16
 * @method LifecycleEvents postLoad(string $method)
17
 * @method LifecycleEvents loadClassMetadata(string $method)
18
 * @method LifecycleEvents onClassMetadataNotFound(string $method)
19
 * @method LifecycleEvents preFlush(string $method)
20
 * @method LifecycleEvents onFlush(string $method)
21
 * @method LifecycleEvents postFlush(string $method)
22
 * @method LifecycleEvents onClear(string $method)
23
 */
24
class LifecycleEvents implements Buildable
25
{
26
    /**
27
     * @var ClassMetadataBuilder
28
     */
29
    private $builder;
30
31
    /**
32
     * @var array
33
     */
34
    private $events = [
35
        Events::preRemove               => [],
36
        Events::postRemove              => [],
37
        Events::prePersist              => [],
38
        Events::postPersist             => [],
39
        Events::preUpdate               => [],
40
        Events::postUpdate              => [],
41
        Events::postLoad                => [],
42
        Events::loadClassMetadata       => [],
43
        Events::onClassMetadataNotFound => [],
44
        Events::preFlush                => [],
45
        Events::onFlush                 => [],
46
        Events::postFlush               => [],
47
        Events::onClear                 => [],
48
    ];
49
50
    /**
51
     * LifecycleEvents constructor.
52
     *
53
     * @param ClassMetadataBuilder $builder
54
     */
55 16
    public function __construct(ClassMetadataBuilder $builder)
56
    {
57 16
        $this->builder = $builder;
58 16
    }
59
60
    /**
61
     * Magically call all methods that match an event name.
62
     *
63
     * @param string $method
64
     * @param array  $args
65
     *
66
     * @throws \InvalidArgumentException
67
     * @return LifecycleEvents
68
     */
69 15
    public function __call($method, $args)
70
    {
71 15
        if (array_key_exists($method, $this->events)) {
72 14
            array_unshift($args, $method);
73
74 14
            return call_user_func_array([$this, 'add'], $args);
75
        }
76
77 1
        throw new \InvalidArgumentException('Fluent builder method [' . $method . '] does not exist');
78
    }
79
80
    /**
81
     * @param string $event
82
     * @param string $method
83
     *
84
     * @return LifecycleEvents
85
     */
86 14
    private function add($event, $method)
87
    {
88 14
        $this->events[$event][] = $method;
89
90 14
        return $this;
91
    }
92
93
    /**
94
     * Execute the build process.
95
     */
96 14
    public function build()
97
    {
98 14
        foreach ($this->events as $event => $methods) {
99 14
            foreach ($methods as $method) {
100 14
                $this->builder->addLifecycleEvent($method, $event);
101 14
            }
102 14
        }
103 14
    }
104
}
105