Passed
Push — master ( 57971d...267513 )
by Ben
14:57 queued 10s
created

UrlSlugFields::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
    public static function fromModel(ProvidesUrl $model)
11
    {
12
        $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
        self::fillWithExistingValues($model, $fields);
15
16
        return $fields;
17
    }
18
19
    public static function redirectsFromModel(ProvidesUrl $model)
20
    {
21
        $records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) {
22
            return !$record->isRedirect();
23
        })->sortByDesc('created_at');
24
25
        $fields = new static([]);
26
27
        foreach ($records as $record) {
28
            $key = 'redirects-'.$record->locale.'-'.$record->slug;
29
            $fields[$key] = UrlSlugField::make($key)
30
                ->setUrlRecord($record)
31
                ->setFullUrl($model->resolveUrl($record->locale, $record->slug));
32
        }
33
34
        return $fields;
35
    }
36
37
    public function toJson(): string
38
    {
39
        return json_encode($this->toArray());
40
    }
41
42
    public function toArray(): array
43
    {
44
        $array = [];
45
46
        foreach ($this->all() as $field) {
47
            $array[] = $field->toArray();
48
        }
49
50
        return $array;
51
    }
52
53
    /**
54
     * @param array $locales
55
     * @param ProvidesUrl $model
56
     * @return UrlSlugFields
57
     */
58
    private static function initEmptyFields(array $locales, ProvidesUrl $model): self
59
    {
60
        $fields = new static([]);
61
62
        foreach ($locales as $locale) {
63
            $fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale)
64
                                                ->setBaseUrlSegment($model->baseUrlSegment($locale))
65
                                                ->prepend($model->resolveUrl($locale, $model->baseUrlSegment($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

65
                                                ->/** @scrutinizer ignore-call */ prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/')
Loading history...
66
                                                ->name('url-slugs[' . $locale . ']')
67
                                                ->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

67
                                                ->/** @scrutinizer ignore-call */ label($locale);
Loading history...
68
        }
69
70
        return $fields;
71
    }
72
73
    /**
74
     * @param ProvidesUrl $model
75
     * @param $fields
76
     */
77
    private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void
78
    {
79
        $records = UrlRecord::getByModel($model)->reject(function ($record) {
80
            return $record->isRedirect();
81
        })->sortBy('locale');
82
83
        foreach ($records as $record) {
84
            if (!isset($fields['url-slugs.'.$record->locale])) {
85
                continue;
86
            }
87
88
            $fields['url-slugs.'.$record->locale]
89
                ->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

89
                ->/** @scrutinizer ignore-call */ 
90
                  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...
90
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
91
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/');
92
        }
93
    }
94
}
95