Completed
Push — extensions-support ( cafd05...735378 )
by Guido
05:02
created

MaterializedPath   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 14
c 5
b 0
f 1
lcom 1
cbo 6
dl 0
loc 152
ccs 48
cts 48
cp 1
rs 10

7 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 8 1
B defaults() 0 16 7
A getValues() 0 17 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 9
    public function path($field = 'path', $separator = '|', callable $callback = null)
71
    {
72 9
        $this->mapField('string', $field);
73
74 9
        $this->path = new TreePath($this->getClassMetadata(), $field, $separator);
75
76 9
        $this->callbackAndQueue($this->path, $callback);
77
78 9
        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 9
    public function pathSource($field = 'id')
101
    {
102 9
        $this->source = $field;
103
104 9
        return $this;
105
    }
106
107
    /**
108
     * Execute the build process
109
     */
110 9
    public function build()
111
    {
112 9
        $this->defaults();
113
114 9
        parent::build();
115
116 9
        $this->buildQueue();
117 9
    }
118
119
    /**
120
     * Add default values to all required fields.
121
     *
122
     * @return void
123
     */
124 9
    private function defaults()
125
    {
126 9
        $config = $this->getClassMetadata()->getExtension($this->getExtensionName());
127
128 9
        if (!$this->path && !isset($config['path'])) {
129 6
            $this->path();
130 6
        }
131
132 9
        if (!$this->parent && !isset($config['parent'])) {
133 8
            $this->parent();
134 8
        }
135
136 9
        if (!$this->source && !isset($config['path_source'])) {
137 8
            $this->pathSource();
138 8
        }
139 9
    }
140
141
    /**
142
     * @return array
143
     */
144 9
    protected function getValues()
145
    {
146 9
        $values = array_merge(parent::getValues(), [
147 9
            'strategy'                   => 'materializedPath',
148 9
            'path_source'                => $this->source,
149 9
            'path_separator'             => $this->separator,
150 9
            'path_append_id'             => $this->appendIds,
151 9
            'path_starts_with_separator' => $this->startsWithSeparator,
152 9
            'path_ends_with_separator'   => $this->endsWithSeparator,
153 9
        ]);
154
155 9
        if ($this->hash) {
156 1
            $values['path_hash'] = $this->hash;
157 1
        }
158
159 9
        return $values;
160
    }
161
}
162