Completed
Pull Request — 1.1 (#57)
by Patrick
25:25 queued 11:44
created

EntityListeners::build()   A

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