Completed
Push — feature/indexable-columns ( ce3da3 )
by Patrick
05:20
created

EntityListeners   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 11.63 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 10
loc 86
ccs 19
cts 20
cp 0.95
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
     *
69
     * @return LifecycleEvents
70
     */
71 16 View Code Duplication
    public function __call($event, $args)
72
    {
73 16
        if (array_key_exists($event, $this->events)) {
74 16
            array_unshift($args, $event);
75
76 16
            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 16
    private function add($event, $class, $method = null)
90
    {
91 16
        $this->events[$event][] = [
92 16
            'class'  => $class,
93 16
            'method' => $method ?: $event,
94
        ];
95
96 16
        return $this;
97
    }
98
99
    /**
100
     * Execute the build process.
101
     */
102 16
    public function build()
103
    {
104 16
        foreach ($this->events as $event => $listeners) {
105 16
            foreach ($listeners as $listener) {
106 16
                $this->builder->getClassMetadata()->addEntityListener($event, $listener['class'], $listener['method']);
107 16
            }
108 16
        }
109 16
    }
110
}
111