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

Builder::listen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use Doctrine\DBAL\Types\Type;
6
use InvalidArgumentException;
7
use LaravelDoctrine\Fluent\Extensions\Gedmo\GedmoBuilderHints;
8
use LaravelDoctrine\Fluent\Fluent;
9
use LogicException;
10
11
/**
12
 * @method Field array($name, callable $callback = null)
13
 */
14
class Builder extends AbstractBuilder implements Fluent
15
{
16
    use Traits\Fields;
17
    use Traits\Dates;
18
    use Traits\Aliases;
19
    use Traits\Relations;
20
    use Traits\Constraints;
21
    use Traits\Macroable;
22
    use Traits\Queueable;
23
    use Traits\QueuesMacros;
24
    use GedmoBuilderHints;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function table($name, callable $callback = null)
30
    {
31 2
        $this->disallowInEmbeddedClasses();
32
33 1
        $table = new Table($this->builder, $name);
34
35 1
        $this->callbackAndQueue($table, $callback);
36
37 1
        return $table;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 39
    public function entity(callable $callback = null)
44
    {
45 39
        $this->disallowInEmbeddedClasses();
46
47 38
        $entity = new Entity($this->builder, $this->namingStrategy);
48
49 38
        $this->callIfCallable($callback, $entity);
50
51 38
        return $entity;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3
    public function inheritance($type, callable $callback = null)
58
    {
59 3
        $inheritance = Inheritance\InheritanceFactory::create($type, $this->builder);
60
61 3
        $this->callIfCallable($callback, $inheritance);
62
63 3
        return $inheritance;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function singleTableInheritance(callable $callback = null)
70
    {
71 1
        return $this->inheritance(Inheritance\Inheritance::SINGLE, $callback);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function joinedTableInheritance(callable $callback = null)
78
    {
79 1
        return $this->inheritance(Inheritance\Inheritance::JOINED, $callback);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function embed($embeddable, $field = null, callable $callback = null)
86
    {
87 2
        $embedded = new Embedded(
88 2
            $this->builder,
89 2
            $this->namingStrategy,
90 2
            $this->guessSingularField($embeddable, $field),
91
            $embeddable
92 2
        );
93
94 2
        $this->callbackAndQueue($embedded, $callback);
95
96 2
        return $embedded;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 3
    public function override($name, callable $callback)
103
    {
104 3
        $override = new Overrides\Override(
105 3
            $this->getBuilder(),
106 3
            $this->getNamingStrategy(),
107 3
            $name,
108
            $callback
109 3
        );
110
111 3
        $this->queue($override);
112
113 3
        return $override;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function events(callable $callback = null)
120
    {
121 2
        $events = new LifecycleEvents($this->builder);
122
123 2
        $this->callbackAndQueue($events, $callback);
124
125 2
        return $events;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 2
    public function listen(callable $callback = null)
132
    {
133 2
        $events = new EntityListeners($this->builder);
134
135 2
        $this->callbackAndQueue($events, $callback);
136
137 2
        return $events;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 50
    public function isEmbeddedClass()
144
    {
145 50
        return $this->builder->getClassMetadata()->isEmbeddedClass;
146
    }
147
148
    /**
149
     * @param string        $name
150
     * @param callable|null $callback
151
     *
152
     * @return Field
153
     */
154 3
    protected function setArray($name, callable $callback = null)
155
    {
156 3
        return $this->field(Type::TARRAY, $name, $callback);
157
    }
158
159
    /**
160
     * @param string $method
161
     * @param array  $params
162
     *
163
     * @return mixed
164
     */
165 96
    public function __call($method, $params)
166
    {
167
        // Workaround for reserved keywords
168 96
        if ($method === 'array') {
169 3
            return call_user_func_array([$this, 'setArray'], $params);
170
        }
171
172 93
        if ($this->hasMacro($method)) {
173 92
            return $this->queueMacro($method, $params);
174
        }
175
176 1
        throw new InvalidArgumentException('Fluent builder method ['.$method.'] does not exist');
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 50
    protected function disallowInEmbeddedClasses($message = '')
183
    {
184 50
        if ($this->isEmbeddedClass()) {
185 5
            throw new LogicException($message);
186
        }
187 45
    }
188
}
189