Completed
Pull Request — master (#22)
by Guido
08:31
created

Builder::entity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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