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