Completed
Push — extensions-support ( 5f703b...dc3ff8 )
by Guido
04:50
created

MaterializedPath::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use LaravelDoctrine\Fluent\Buildable;
6
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
7
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
8
use LaravelDoctrine\Fluent\Extensions\Extension;
9
10
class MaterializedPath extends TreeStrategy implements Buildable, Extension
11
{
12
    use Queueable;
13
14
    /**
15
     * @var string
16
     */
17
    private $path;
18
19
    /**
20
     * @var string
21
     */
22 2
    private $hash;
23
24 2
    /**
25 2
     * @var string
26
     */
27
    private $source;
28
29
    /**
30 1
     * @var string
31
     */
32
    private $separator;
33 1
34
    /**
35 1
     * @var bool
36 1
     */
37 1
    private $appendIds;
38 1
39
    /**
40
     * @var bool
41
     */
42
    private $startsWithSeparator = false;
43 1
44
    /**
45 1
     * @var bool
46
     */
47
    private $endsWithSeparator = true;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public static function enable()
53
    {
54
        TreePath::enable();
55
        TreePathHash::enable();
56
        TreePathSource::enable();
57
    }
58
59
    /**
60
     * @param string        $field
61
     * @param string        $separator
62
     * @param callable|null $callback
63
     *
64
     * @return $this
65
     */
66
    public function path($field = 'path', $separator = '|', callable $callback = null)
67
    {
68
        $this->mapField('string', $field);
69
70
        $this->path = new TreePath($this->getClassMetadata(), $field, $separator);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \LaravelDoctrine\Flu...(), $field, $separator) of type object<LaravelDoctrine\F...ensions\Gedmo\TreePath> is incompatible with the declared type string of property $path.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
72
        $this->callbackAndQueue($this->path, $callback);
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $hash
79
     *
80
     * @return $this
81
     */
82
    public function pathHash($hash = 'pathHash')
83
    {
84
        $this->mapField('string', $hash);
85
86
        $this->hash = $hash;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $field
93
     *
94
     * @return $this
95
     */
96
    public function pathSource($field = 'id')
97
    {
98
        $this->source = $field;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Execute the build process
105
     */
106
    public function build()
107
    {
108
        $this->defaults();
109
110
        /** @var ExtensibleClassMetadata $classMetadata */
111
        $classMetadata = $this->getClassMetadata();
112
        $classMetadata->mergeExtension($this->getExtensionName(), $this->getValues());
113
114
        foreach ($this->getQueued() as $queued) {
115
            $queued->build();
116
        }
117
    }
118
119
    /**
120
     * Add default values to all required fields.
121
     *
122
     * @return void
123
     */
124
    private function defaults()
125
    {
126
        if (!$this->path) {
127
            $this->path();
128
        }
129
130
        if (!$this->parent) {
131
            $this->parent();
132
        }
133
134
        if (!$this->source) {
135
            $this->pathSource();
136
        }
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    protected function getValues()
143
    {
144
        $values = array_merge(parent::getValues(), [
145
            'strategy'                   => 'materializedPath',
146
            'path_source'                => $this->source,
147
            'path_separator'             => $this->separator,
148
            'path_append_id'             => $this->appendIds,
149
            'path_starts_with_separator' => $this->startsWithSeparator,
150
            'path_ends_with_separator'   => $this->endsWithSeparator,
151
        ]);
152
153
        if ($this->hash) {
154
            $values['path_hash'] = $this->hash;
155
        }
156
157
        return $values;
158
    }
159
}
160