Builder   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 28
Bugs 0 Features 9
Metric Value
wmc 26
c 28
b 0
f 9
lcom 1
cbo 15
dl 0
loc 248
ccs 82
cts 82
cp 1
rs 9.1666

16 Methods

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