Test Failed
Push — fix/localized-img-bug ( a16f3e...daa57d )
by Ben
21:18
created

UrlSlugFields::initEmptyFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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
    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
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
32
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) . ($record->slug != '/' ? '/' : ''));
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)) . ($record->slug != '/' ? '/' : ''));
Loading history...
33
        }
34
35
        return $fields;
36
    }
37
38
    public function toJson(): string
39
    {
40
        return json_encode($this->toArray());
41
    }
42
43
    public function toArray(): array
44
    {
45
        $array = [];
46
47
        foreach ($this->all() as $field) {
48
            $array[] = $field->toArray();
49
        }
50
51
        return $array;
52
    }
53
54
    /**
55
     * @param array $locales
56
     * @param ProvidesUrl $model
57
     * @return UrlSlugFields
58
     */
59
    private static function initEmptyFields(array $locales, ProvidesUrl $model): self
60
    {
61
        $fields = new static([]);
62
63
        foreach ($locales as $locale) {
64
            $fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale)
65
                                                ->setBaseUrlSegment($model->baseUrlSegment($locale))
66
                                                ->prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/')
67
                                                ->name('url-slugs[' . $locale . ']')
68
                                                ->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
        return $fields;
72
    }
73
74
    /**
75
     * @param ProvidesUrl $model
76
     * @param $fields
77
     */
78
    private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void
79
    {
80
        $records = UrlRecord::getByModel($model)->reject(function ($record) {
81
            return $record->isRedirect();
82
        })->sortBy('locale');
83
84
        foreach ($records as $record) {
85
            if (!isset($fields['url-slugs.'.$record->locale])) {
86
                continue;
87
            }
88
89
            $fields['url-slugs.'.$record->locale]
90
                ->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
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
92
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/');
93
        }
94
    }
95
}
96