Completed
Push — 1.1 ( d166b0...e7f438 )
by Patrick
11:31 queued 07:46
created

Builder   B

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 18

Importance

Changes 32
Bugs 0 Features 10
Metric Value
wmc 15
c 32
b 0
f 10
lcom 2
cbo 18
dl 0
loc 163
rs 7.3333

12 Methods

Rating   Name   Duplication   Size   Complexity  
A table() 0 10 1
A entity() 0 10 1
A inheritance() 0 8 1
A singleTableInheritance() 0 4 1
A joinedTableInheritance() 0 4 1
A embed() 0 13 1
A override() 0 13 1
A events() 0 8 1
A isEmbeddedClass() 0 4 1
A setArray() 0 4 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\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
    public function table($name, callable $callback = null)
30
    {
31
        $this->disallowInEmbeddedClasses();
32
33
        $table = new Table($this->builder, $name);
34
35
        $this->callIfCallable($callback, $table);
36
37
        return $table;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function entity(callable $callback = null)
44
    {
45
        $this->disallowInEmbeddedClasses();
46
47
        $entity = new Entity($this->builder, $this->namingStrategy);
48
49
        $this->callIfCallable($callback, $entity);
50
51
        return $entity;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function inheritance($type, callable $callback = null)
58
    {
59
        $inheritance = Inheritance\InheritanceFactory::create($type, $this->builder);
60
61
        $this->callIfCallable($callback, $inheritance);
62
63
        return $inheritance;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function singleTableInheritance(callable $callback = null)
70
    {
71
        return $this->inheritance(Inheritance\Inheritance::SINGLE, $callback);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function joinedTableInheritance(callable $callback = null)
78
    {
79
        return $this->inheritance(Inheritance\Inheritance::JOINED, $callback);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function embed($embeddable, $field = null, callable $callback = null)
86
    {
87
        $embedded = new Embedded(
88
            $this->builder,
89
            $this->namingStrategy,
90
            $this->guessSingularField($embeddable, $field),
91
            $embeddable
92
        );
93
94
        $this->callbackAndQueue($embedded, $callback);
95
96
        return $embedded;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function override($name, callable $callback)
103
    {
104
        $override = new Overrides\Override(
105
            $this->getBuilder(),
106
            $this->getNamingStrategy(),
107
            $name,
108
            $callback
109
        );
110
111
        $this->queue($override);
112
113
        return $override;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function events(callable $callback = null)
120
    {
121
        $events = new LifecycleEvents($this->builder);
122
123
        $this->callbackAndQueue($events, $callback);
124
125
        return $events;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function isEmbeddedClass()
132
    {
133
        return $this->builder->getClassMetadata()->isEmbeddedClass;
134
    }
135
136
    /**
137
     * @param string        $name
138
     * @param callable|null $callback
139
     *
140
     * @return Field
141
     */
142
    protected function setArray($name, callable $callback = null)
143
    {
144
        return $this->field(Type::TARRAY, $name, $callback);
145
    }
146
147
    /**
148
     * @param string $method
149
     * @param array  $params
150
     *
151
     * @return mixed
152
     */
153
    public function __call($method, $params)
154
    {
155
        // Workaround for reserved keywords
156
        if ($method === 'array') {
157
            return call_user_func_array([$this, 'setArray'], $params);
158
        }
159
160
        if ($this->hasMacro($method)) {
161
            return $this->queueMacro($method, $params);
162
        }
163
164
        throw new InvalidArgumentException('Fluent builder method [' . $method . '] does not exist');
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    protected function disallowInEmbeddedClasses($message = "")
171
    {
172
        if ($this->isEmbeddedClass()) {
173
            throw new LogicException($message);
174
        }
175
    }
176
}
177