Passed
Push — 0.3 ( 9ef111...bea7df )
by Philippe
48:58 queued 40:35
created

UrlSlugField::setFullUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    private $fullUrl;
14
15 21
    public function setUrlRecord(UrlRecord $urlRecord)
16
    {
17 21
        $this->urlRecord = $urlRecord;
18
19
        return $this;
20
    }
21
22
    public function getUrlRecordId(): int
23
    {
24
        return $this->urlRecord->id;
25 70
    }
26
27 70
    public function setBaseUrlSegment($baseUrlSegment = null)
28
    {
29 70
        $this->baseUrlSegment = $baseUrlSegment;
30
31
        return $this;
32 4
    }
33
34 4
    public function value()
35
    {
36
        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...
37
    }
38
39
    public function fullUrl(): string
40
    {
41
        return $this->fullUrl
42 4
            ? $this->fullUrl
43
            : $this->prepend.$this->value();
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...
44 4
    }
45
46
    public function setFullUrl(string $fullUrl)
47
    {
48 4
        $this->fullUrl = $fullUrl;
49
50
        return $this;
51
    }
52 4
53
    private function rawSlugValue(): string
54
    {
55
        if (!$this->urlRecord) {
56 4
            return '';
57 2
        }
58
59
        $slug = $this->urlRecord->slug;
60 4
61
        // If this is a '/' slug, it indicates the homepage for this locale. In this case,
62
        // we wont be trimming the slash
63
        if ($slug === '/') {
64
            return $slug;
65
        }
66
67 4
        if ($this->startsWithBaseUrlSegment($slug)) {
68
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
69 4
        }
70
71
        return $slug;
72 1
    }
73
74 1
    /**
75 1
     * @param $value
76 1
     * @return bool
77 1
     */
78 1
    private function startsWithBaseUrlSegment($value): bool
79 1
    {
80 1
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
81 1
    }
82
83 1
    public function toArray(): array
84 1
    {
85
        return array_merge($this->values, [
86
            '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...
87
            '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...
88
            '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...
89
            '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...
90
            '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...
91
            'value' => $this->value(),
92
            'baseUrlSegment' => $this->baseUrlSegment,
93
            'hint' => null, // Hint placeholder to show url hint when it already exists
94
            'is_homepage' => ($this->value() === '/'),
95
            'show' => !!$this->value(),// show input field or not
96
        ]);
97
    }
98
}
99