Completed
Push — feature/entityListeners ( 65e37d )
by Patrick
04:01
created

EntityListeners   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 10
loc 85
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 10 10 2
A add() 0 9 2
A build() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 17
    public function __construct(ClassMetadataBuilder $builder)
57
    {
58 17
        $this->builder = $builder;
59 17
    }
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
     * @return LifecycleEvents
69
     */
70 16 View Code Duplication
    public function __call($event, $args)
71
    {
72 16
        if (array_key_exists($event, $this->events)) {
73 16
            array_unshift($args, $event);
74
75 16
            return call_user_func_array([$this, 'add'], $args);
76
        }
77
78
        throw new InvalidArgumentException('Fluent builder method [' . $event . '] does not exist');
79
    }
80
81
    /**
82
     * @param string      $event
83
     * @param string      $class
84
     * @param string|null $method
85
     *
86
     * @return EntityListeners
87
     */
88 16
    private function add($event, $class, $method = null)
89
    {
90 16
        $this->events[$event][] = [
91 16
            'class'  => $class,
92
            'method' => $method ?: $event
93 16
        ];
94
95 16
        return $this;
96
    }
97
98
    /**
99
     * Execute the build process.
100
     */
101 16
    public function build()
102
    {
103 16
        foreach ($this->events as $event => $listeners) {
104 16
            foreach ($listeners as $listener) {
105 16
                $this->builder->getClassMetadata()->addEntityListener($event, $listener['class'], $listener['method']);
106 16
            }
107 16
        }
108 16
    }
109
}
110