Passed
Push — 0.3 ( 9a1703...7b1086 )
by Philippe
42:07
created

UrlSlugFields   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 80.48%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
eloc 38
c 3
b 1
f 0
dl 0
loc 85
ccs 33
cts 41
cp 0.8048
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 7 1
A fillWithExistingValues() 0 15 3
A redirectsFromModel() 0 17 2
A initEmptyFields() 0 13 2
A toJson() 0 3 1
A toArray() 0 9 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Thinktomorrow\Chief\Fields\Fields;
6
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl;
7
8
class UrlSlugFields extends Fields
9
{
10 66
    public static function fromModel(ProvidesUrl $model)
11
    {
12 66
        $fields = self::initEmptyFields($model->availableLocales(), $model);
0 ignored issues
show
Bug introduced by
The method availableLocales() does not exist on Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        $fields = self::initEmptyFields($model->/** @scrutinizer ignore-call */ availableLocales(), $model);
Loading history...
13
14 66
        self::fillWithExistingValues($model, $fields);
15
16 66
        return $fields;
17
    }
18
19 1
    public static function redirectsFromModel(ProvidesUrl $model)
20
    {
21
        $records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) {
22 1
            return !$record->isRedirect();
23 1
        })->sortByDesc('created_at');
24
25 1
        $fields = new static([]);
26
27 1
        foreach ($records as $record) {
28
            $key = 'redirects-'.$record->locale.'-'.$record->slug;
29
            $fields[$key] = UrlSlugField::make($key)
30
                ->setUrlRecord($record)
31
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
32
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) . '/');
0 ignored issues
show
Bug introduced by
The method prepend() does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                ->/** @scrutinizer ignore-call */ prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) . '/');
Loading history...
33
        }
34
35 1
        return $fields;
36
    }
37
38
    public function toJson(): string
39
    {
40
        return json_encode($this->toArray());
41
    }
42
43 1
    public function toArray(): array
44
    {
45 1
        $array = [];
46
47 1
        foreach ($this->all() as $field) {
48 1
            $array[] = $field->toArray();
49
        }
50
51 1
        return $array;
52
    }
53
54
    /**
55
     * @param array $locales
56
     * @param ProvidesUrl $model
57
     * @return UrlSlugFields
58
     */
59 66
    private static function initEmptyFields(array $locales, ProvidesUrl $model): self
60
    {
61 66
        $fields = new static([]);
62
63 66
        foreach ($locales as $locale) {
64 66
            $fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale)
65 66
                                                ->setBaseUrlSegment($model->baseUrlSegment($locale))
66 66
                                                ->prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/')
67 66
                                                ->name('url-slugs[' . $locale . ']')
0 ignored issues
show
Bug introduced by
The method name() does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
                                                ->/** @scrutinizer ignore-call */ name('url-slugs[' . $locale . ']')
Loading history...
68 66
                                                ->label($locale);
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                                                ->/** @scrutinizer ignore-call */ label($locale);
Loading history...
69
        }
70
71 66
        return $fields;
72
    }
73
74
    /**
75
     * @param ProvidesUrl $model
76
     * @param $fields
77
     */
78 66
    private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void
79
    {
80
        $records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) {
81 21
            return $record->isRedirect();
82 66
        })->sortBy('locale');
83
84 66
        foreach ($records as $record) {
85 21
            if (!isset($fields['url-slugs.'.$record->locale])) {
86
                continue;
87
            }
88
89 21
            $fields['url-slugs.'.$record->locale]
90 21
                ->setUrlRecord($record)
0 ignored issues
show
Bug introduced by
The method setUrlRecord() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
                ->/** @scrutinizer ignore-call */ 
91
                  setUrlRecord($record)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91 21
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
92 21
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/');
93
        }
94 66
    }
95
}
96