Completed
Push — extensions-support ( 0f14db...20cfb1 )
by Guido
03:45
created

Builder::unique()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 2
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use Doctrine\DBAL\Types\Type;
6
use InvalidArgumentException;
7
use LaravelDoctrine\Fluent\Extensions\Gedmo\GedmoBuilderHints;
8
use LaravelDoctrine\Fluent\Fluent;
9
use LogicException;
10
11
/**
12
 * @method Field array($name, callable $callback = null)
13
 */
14
class Builder extends AbstractBuilder implements Fluent
15
{
16
    use Traits\Fields;
17
    use Traits\Dates;
18
    use Traits\Aliases;
19
    use Traits\Relations;
20
    use Traits\Constraints;
21
    use Traits\Macroable;
22
    use Traits\Queueable;
23
    use Traits\QueuesMacros;
24
    use GedmoBuilderHints;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function table($name, callable $callback = null)
30
    {
31 2
        $this->disallowInEmbeddedClasses();
32
33 1
        $table = new Table($this->builder, $name);
34
35 1
        $this->callIfCallable($callback, $table);
36
37 1
        return $table;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3
    public function entity(callable $callback = null)
44
    {
45 3
        $this->disallowInEmbeddedClasses();
46
47 2
        $entity = new Entity($this->builder, $this->namingStrategy);
48
49 2
        $this->callIfCallable($callback, $entity);
50
51 2
        return $entity;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3
    public function inheritance($type, callable $callback = null)
58
    {
59 3
        $inheritance = Inheritance\InheritanceFactory::create($type, $this->builder);
60
61 3
        $this->callIfCallable($callback, $inheritance);
62
63 3
        return $inheritance;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function singleTableInheritance(callable $callback = null)
70
    {
71 1
        return $this->inheritance(Inheritance\Inheritance::SINGLE, $callback);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function joinedTableInheritance(callable $callback = null)
78
    {
79 1
        return $this->inheritance(Inheritance\Inheritance::JOINED, $callback);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function embed($embeddable, $field = null, callable $callback = null)
86
    {
87 2
        $embedded = new Embedded(
88 2
            $this->builder,
89 2
            $this->namingStrategy,
90 2
            $this->guessSingularField($embeddable, $field),
91
            $embeddable
92 2
        );
93
94 2
        $this->callbackAndQueue($embedded, $callback);
95
96 2
        return $embedded;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 3
    public function override($name, callable $callback)
103
    {
104 3
        $override = new Overrides\Override(
105 3
            $this->getBuilder(),
106 3
            $this->getNamingStrategy(),
107 3
            $name,
108
            $callback
109 3
        );
110
111 3
        $this->queue($override);
112
113 3
        return $override;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function events(callable $callback = null)
120
    {
121 2
        $events = new LifecycleEvents($this->builder);
122
123 2
        $this->callbackAndQueue($events, $callback);
124
125 2
        return $events;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 14
    public function isEmbeddedClass()
132
    {
133 14
        return $this->builder->getClassMetadata()->isEmbeddedClass;
134
    }
135
136
    /**
137
     * @param string        $name
138
     * @param callable|null $callback
139
     *
140
     * @return Field
141
     */
142 3
    protected function setArray($name, callable $callback = null)
143
    {
144 3
        return $this->field(Type::TARRAY, $name, $callback);
145
    }
146
147
    /**
148
     * @param string $method
149
     * @param array  $params
150
     *
151
     * @return mixed
152
     */
153 87
    public function __call($method, $params)
154
    {
155
        // Workaround for reserved keywords
156 87
        if ($method === 'array') {
157 3
            return call_user_func_array([$this, 'setArray'], $params);
158
        }
159
160 84
        if ($this->hasMacro($method)) {
161 83
            return $this->queueMacro($method, $params);
162
        }
163
164 1
        throw new InvalidArgumentException('Fluent builder method [' . $method . '] does not exist');
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 14
    protected function disallowInEmbeddedClasses($message = "")
171
    {
172 14
        if ($this->isEmbeddedClass()) {
173 5
            throw new LogicException($message);
174
        }
175 9
    }
176
}
177