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

MaterializedPath   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 14
c 7
b 0
f 1
lcom 1
cbo 5
dl 0
loc 163
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A enable() 0 8 1
A path() 0 10 1
A pathHash() 0 8 1
A pathSource() 0 6 1
A build() 0 6 1
A defaults() 0 12 3
A getValues() 0 18 2
A isMissingPath() 0 4 2
A isMissingSource() 0 4 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
    public static function enable()
55
    {
56
        parent::enable();
57
58
        TreePath::enable();
59
        TreePathHash::enable();
60
        TreePathSource::enable();
61
    }
62
63
    /**
64
     * @param string        $field
65
     * @param string        $separator
66
     * @param callable|null $callback
67
     *
68
     * @return $this
69
     */
70
    public function path($field = 'path', $separator = '|', callable $callback = null)
71
    {
72
        $this->mapField('string', $field, null, true);
73
74
        $this->path = new TreePath($this->getClassMetadata(), $field, $separator);
75
76
        $this->callbackAndQueue($this->path, $callback);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $hash
83
     *
84
     * @return $this
85
     */
86
    public function pathHash($hash = 'pathHash')
87
    {
88
        $this->mapField('string', $hash);
89
90
        $this->hash = $hash;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $field
97
     *
98
     * @return $this
99
     */
100
    public function pathSource($field = 'id')
101
    {
102
        $this->source = $field;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Execute the build process
109
     */
110
    public function build()
111
    {
112
        parent::build();
113
114
        $this->buildQueue();
115
    }
116
117
    /**
118
     * Add default values to all required fields.
119
     *
120
     * @return void
121
     */
122
    protected function defaults()
123
    {
124
        parent::defaults();
125
126
        if ($this->isMissingPath()) {
127
            $this->path();
128
        }
129
130
        if ($this->isMissingSource()) {
131
            $this->pathSource();
132
        }
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    protected function getValues()
139
    {
140
        $values = array_merge(parent::getValues(), [
141
            'strategy'                   => 'materializedPath',
142
            'path_source'                => $this->source,
143
            'path_separator'             => $this->separator,
144
            'path_append_id'             => $this->appendIds,
145
            'path_starts_with_separator' => $this->startsWithSeparator,
146
            'path_ends_with_separator'   => $this->endsWithSeparator,
147
            'activate_locking'           => false,
148
        ]);
149
150
        if ($this->hash) {
151
            $values['path_hash'] = $this->hash;
152
        }
153
154
        return $values;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    private function isMissingPath()
161
    {
162
        return !$this->alreadyConfigured('path') && !$this->path;
163
    }
164
165
    /**
166
     * @return bool
167
     */
168
    private function isMissingSource()
169
    {
170
        return !$this->alreadyConfigured('path_source') && !$this->source;
171
    }
172
}
173