Completed
Push — extensions-support ( 242eac...f580fa )
by Guido
06:26
created

MaterializedPath::isMissingPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use LaravelDoctrine\Fluent\Buildable;
6
use LaravelDoctrine\Fluent\Builders\Delay;
7
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
8
use LaravelDoctrine\Fluent\Extensions\Extension;
9
10
class MaterializedPath extends TreeStrategy implements Buildable, Extension, Delay
11
{
12
    use Queueable {
13
        build as buildQueue;
14
    }
15
16
    /**
17
     * @var null|TreePath
18
     */
19
    private $path;
20
21
    /**
22
     * @var string
23
     */
24
    private $hash;
25
26
    /**
27
     * @var string
28
     */
29
    private $source;
30
31
    /**
32
     * @var string
33
     */
34
    private $separator;
35
36
    /**
37
     * @var bool
38
     */
39
    private $appendIds;
40
41
    /**
42
     * @var bool
43
     */
44
    private $startsWithSeparator = false;
45
46
    /**
47
     * @var bool
48
     */
49
    private $endsWithSeparator = true;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 85
    public static function enable()
55
    {
56 85
        parent::enable();
57
58 85
        TreePath::enable();
59 85
        TreePathHash::enable();
60 85
        TreePathSource::enable();
61 85
    }
62
63
    /**
64
     * @param string        $field
65
     * @param string        $separator
66
     * @param callable|null $callback
67
     *
68
     * @return $this
69
     */
70 10
    public function path($field = 'path', $separator = '|', callable $callback = null)
71
    {
72 10
        $this->mapField('string', $field);
73
74 10
        $this->path = new TreePath($this->getClassMetadata(), $field, $separator);
75
76 10
        $this->callbackAndQueue($this->path, $callback);
77
78 10
        return $this;
79
    }
80
81
    /**
82
     * @param string $hash
83
     *
84
     * @return $this
85
     */
86 1
    public function pathHash($hash = 'pathHash')
87
    {
88 1
        $this->mapField('string', $hash);
89
90 1
        $this->hash = $hash;
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * @param string $field
97
     *
98
     * @return $this
99
     */
100 10
    public function pathSource($field = 'id')
101
    {
102 10
        $this->source = $field;
103
104 10
        return $this;
105
    }
106
107
    /**
108
     * Execute the build process
109
     */
110 10
    public function build()
111
    {
112 10
        parent::build();
113
114 10
        $this->buildQueue();
115 10
    }
116
117
    /**
118
     * Add default values to all required fields.
119
     *
120
     * @return void
121
     */
122 10
    protected function defaults()
123
    {
124 10
        parent::defaults();
125
126 10
        if ($this->isMissingPath()) {
127 7
            $this->path();
128 7
        }
129
130 10
        if ($this->isMissingSource()) {
131 9
            $this->pathSource();
132 9
        }
133 10
    }
134
135
    /**
136
     * @return array
137
     */
138 10
    protected function getValues()
139
    {
140 10
        $values = array_merge(parent::getValues(), [
141 10
            'strategy'                   => 'materializedPath',
142 10
            'path_source'                => $this->source,
143 10
            'path_separator'             => $this->separator,
144 10
            'path_append_id'             => $this->appendIds,
145 10
            'path_starts_with_separator' => $this->startsWithSeparator,
146 10
            'path_ends_with_separator'   => $this->endsWithSeparator,
147 10
            'activate_locking'           => false,
148 10
        ]);
149
150 10
        if ($this->hash) {
151 1
            $values['path_hash'] = $this->hash;
152 1
        }
153
154 10
        return $values;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160 10
    private function isMissingPath()
161
    {
162 10
        return !$this->alreadyConfigured('path') && !$this->path;
163
    }
164
165
    /**
166
     * @return bool
167
     */
168 10
    private function isMissingSource()
169
    {
170 10
        return !$this->alreadyConfigured('path_source') && !$this->source;
171
    }
172
}
173