Completed
Pull Request — master (#22)
by Guido
13:59
created

Builder::disallowInEmbeddedClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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