Completed
Push — master ( e03259...350da1 )
by Patrick
10:36 queued 05:10
created

Builder   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 26
Bugs 0 Features 9
Metric Value
wmc 25
c 26
b 0
f 9
lcom 1
cbo 13
dl 0
loc 225
ccs 78
cts 78
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A table() 0 20 4
A entity() 0 14 3
A inheritance() 0 10 2
A singleTableInheritance() 0 4 1
A joinedTableInheritance() 0 4 1
A index() 0 7 2
A primary() 0 7 2
A unique() 0 7 2
A constraint() 0 8 1
A embed() 0 13 1
A override() 0 13 1
A events() 0 8 1
A isEmbeddedClass() 0 4 1
A __call() 0 13 3
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use InvalidArgumentException;
6
use LaravelDoctrine\Fluent\Builders\Inheritance\Inheritance;
7
use LaravelDoctrine\Fluent\Builders\Inheritance\InheritanceFactory;
8
use LaravelDoctrine\Fluent\Builders\Overrides\Override;
9
use LaravelDoctrine\Fluent\Builders\Traits\Fields;
10
use LaravelDoctrine\Fluent\Builders\Traits\Macroable;
11
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
12
use LaravelDoctrine\Fluent\Builders\Traits\Relations;
13
use LaravelDoctrine\Fluent\Fluent;
14
use LogicException;
15
16
/**
17
 * @method $this array($name, callable $callback = null)
18
 */
19
class Builder extends AbstractBuilder implements Fluent
20
{
21
    use Fields, Relations, Macroable, Queueable;
22
23
    /**
24
     * @param string|callable $name
25
     * @param callable|null   $callback
26
     *
27
     * @return Table
28
     */
29 2
    public function table($name, callable $callback = null)
30
    {
31 2
        if ($this->isEmbeddedClass()) {
32 1
            throw new LogicException();
33
        }
34
35 1
        $table = new Table($this->builder);
36
37 1
        if (is_callable($name)) {
38 1
            $name($table);
39 1
        } else {
40 1
            $table->setName($name);
41
        }
42
43 1
        if (is_callable($callback)) {
44 1
            $callback($table);
45 1
        }
46
47 1
        return $table;
48
    }
49
50
    /**
51
     * @param callable|null $callback
52
     *
53
     * @return Entity
54
     */
55 3
    public function entity(callable $callback = null)
56
    {
57 3
        if ($this->isEmbeddedClass()) {
58 1
            throw new LogicException();
59
        }
60
61 2
        $entity = new Entity($this->builder, $this->namingStrategy);
62
63 2
        if (is_callable($callback)) {
64 1
            $callback($entity);
65 1
        }
66
67 2
        return $entity;
68
    }
69
70
    /**
71
     * @param string        $type
72
     * @param callable|null $callback
73
     *
74
     * @return Inheritance
75
     */
76 3
    public function inheritance($type, callable $callback = null)
77
    {
78 3
        $inheritance = InheritanceFactory::create($type, $this->builder);
79
80 3
        if (is_callable($callback)) {
81 3
            $callback($inheritance);
82 3
        }
83
84 3
        return $inheritance;
85
    }
86
87
    /**
88
     * @param callable|null $callback
89
     *
90
     * @return Inheritance
91
     */
92 1
    public function singleTableInheritance(callable $callback = null)
93
    {
94 1
        return $this->inheritance(Inheritance::SINGLE, $callback);
95
    }
96
97
    /**
98
     * @param callable|null $callback
99
     *
100
     * @return Inheritance
101
     */
102 1
    public function joinedTableInheritance(callable $callback = null)
103
    {
104 1
        return $this->inheritance(Inheritance::JOINED, $callback);
105
    }
106
107
    /**
108
     * @param array|string $columns
109
     *
110
     * @return Index
111
     */
112 3
    public function index($columns)
113
    {
114 3
        return $this->constraint(
0 ignored issues
show
Bug Compatibility introduced by Guido Contreras Woda
The expression $this->constraint(\Larav...mns : func_get_args()); of type LaravelDoctrine\Fluent\B...Fluent\Builders\Primary adds the type LaravelDoctrine\Fluent\Builders\Primary to the return on line 114 which is incompatible with the return type declared by the interface LaravelDoctrine\Fluent\Fluent::index of type LaravelDoctrine\Fluent\Builders\Index.
Loading history...
115
            Index::class,
116 3
            is_array($columns) ? $columns : func_get_args()
117 3
        );
118
    }
119 3
120
    /**
121 3
     * @param array|string $fields
122
     *
123 3
     * @return Primary
124
     */
125
    public function primary($fields)
126
    {
127
        return $this->constraint(
0 ignored issues
show
Bug Compatibility introduced by Guido Contreras Woda
The expression $this->constraint(\Larav...lds : func_get_args()); of type LaravelDoctrine\Fluent\B...Fluent\Builders\Primary adds the type LaravelDoctrine\Fluent\Builders\Index to the return on line 127 which is incompatible with the return type declared by the interface LaravelDoctrine\Fluent\Fluent::primary of type LaravelDoctrine\Fluent\Builders\Primary.
Loading history...
128
            Primary::class,
129
            is_array($fields) ? $fields : func_get_args()
130
        );
131 3
    }
132
133 3
    /**
134
     * @param array|string $columns
135 3
     *
136 3
     * @return UniqueConstraint
137
     */
138 3
    public function unique($columns)
139
    {
140 3
        return $this->constraint(
0 ignored issues
show
Bug Compatibility introduced by Guido Contreras Woda
The expression $this->constraint(\Larav...mns : func_get_args()); of type LaravelDoctrine\Fluent\B...Fluent\Builders\Primary adds the type LaravelDoctrine\Fluent\Builders\Primary to the return on line 140 which is incompatible with the return type declared by the interface LaravelDoctrine\Fluent\Fluent::unique of type LaravelDoctrine\Fluent\Builders\UniqueConstraint.
Loading history...
141
            UniqueConstraint::class,
142 3
            is_array($columns) ? $columns : func_get_args()
143
        );
144
    }
145
146
    /**
147
     * @param string $class
148
     * @param array  $columns
149
     *
150 3
     * @return Index|Primary|UniqueConstraint
151
     */
152 3
    protected function constraint($class, array $columns)
153
    {
154 3
        $constraint = new $class($this->builder, $columns);
155 3
156
        $this->queue($constraint);
157 3
158
        return $constraint;
159 3
    }
160
161 3
    /**
162
     * @param string        $embeddable
163
     * @param string|null   $field
164
     * @param callable|null $callback
165
     *
166
     * @return Embedded
167
     */
168
    public function embed($embeddable, $field = null, callable $callback = null)
169
    {
170
        $embedded = new Embedded(
171 2
            $this->builder,
172
            $this->namingStrategy,
173 2
            $this->guessSingularField($embeddable, $field),
174 2
            $embeddable
175 2
        );
176 2
177
        $this->callbackAndQueue($embedded, $callback);
178 2
179
        return $embedded;
180 2
    }
181
182 2
    /**
183
     * @param string   $name
184
     * @param callable $callback
185
     *
186
     * @return Override
187
     */
188
    public function override($name, callable $callback)
189
    {
190
        $override = new Override(
191 3
            $this->getBuilder(),
192
            $this->getNamingStrategy(),
193 3
            $name,
194 3
            $callback
195 3
        );
196 3
197
        $this->queue($override);
198 3
199
        return $override;
200 3
    }
201
202 3
    /**
203
     * @param callable|null $callback
204
     *
205
     * @return LifecycleEvents
206
     */
207
    public function events(callable $callback = null)
208
    {
209
        $events = new LifecycleEvents($this->builder);
210 2
211
        $this->callbackAndQueue($events, $callback);
212 2
213
        return $events;
214 2
    }
215
216 2
    /**
217
     * @return bool
218
     */
219
    public function isEmbeddedClass()
220
    {
221
        return $this->builder->getClassMetadata()->isEmbeddedClass;
222 14
    }
223
224 14
    /**
225
     * @param string $method
226
     * @param array  $params
227
     *
228
     * @return mixed
229
     */
230
    public function __call($method, $params)
231
    {
232
        // Workaround for reserved keywords
233 7
        if ($method === 'array') {
234
            return call_user_func_array([$this, 'setArray'], $params);
235
        }
236 7
237 3
        if ($this->hasMacro($method)) {
238
            return $this->callMacro($method, $params);
239
        }
240 4
241 3
        throw new InvalidArgumentException('Fluent builder method [' . $method . '] does not exist');
242
    }
243
}
244