Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

UrlSlugField   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
eloc 24
c 2
b 1
f 0
dl 0
loc 60
ccs 25
cts 26
cp 0.9615
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 11 1
A setBaseUrlSegment() 0 5 1
A setUrlRecord() 0 5 1
A value() 0 3 1
A startsWithBaseUrlSegment() 0 3 2
A rawSlugValue() 0 13 3
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Thinktomorrow\Chief\Fields\Types\InputField;
6
7
class UrlSlugField extends InputField
8
{
9
    private $urlRecord;
10
11
    private $baseUrlSegment;
12
13 21
    public function setUrlRecord(UrlRecord $urlRecord)
14
    {
15 21
        $this->urlRecord = $urlRecord;
16
17 21
        return $this;
18
    }
19
20 66
    public function setBaseUrlSegment($baseUrlSegment = null)
21
    {
22 66
        $this->baseUrlSegment = $baseUrlSegment;
23
24 66
        return $this;
25
    }
26
27 4
    public function value()
28
    {
29 4
        return old($this->key, $this->rawSlugValue());
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
30
    }
31
32 4
    private function rawSlugValue(): string
33
    {
34 4
        if (!$this->urlRecord) {
35
            return '';
36
        }
37
38 4
        $slug = $this->urlRecord->slug;
39
40 4
        if ($this->startsWithBaseUrlSegment($slug)) {
41 2
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
42
        }
43
44 4
        return $slug;
45
    }
46
47
    /**
48
     * @param $value
49
     * @return bool
50
     */
51 4
    private function startsWithBaseUrlSegment($value): bool
52
    {
53 4
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
54
    }
55
56 1
    public function toArray(): array
57
    {
58 1
        return array_merge($this->values, [
59 1
            'key' => $this->key,
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
60 1
            'prepend' => $this->prepend,
0 ignored issues
show
Bug Best Practice introduced by
The property prepend does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
61 1
            'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
62 1
            'placeholder' => $this->placeholder,
0 ignored issues
show
Bug Best Practice introduced by
The property placeholder does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
63 1
            'description' => $this->description,
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
64 1
            'value' => $this->value(),
65 1
            'locale' => $this->locale,
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
            'hint' => null, // Hint placeholder to show url hint when it already exists
67
        ]);
68
    }
69
}
70