Passed
Push — ft/urls ( 560a84...fa7ece )
by Ben
23:24 queued 11s
created

UrlSlugFields::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 65
    public static function fromModel(ProvidesUrl $model)
11
    {
12 65
        $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 65
        self::fillWithExistingValues($model, $fields);
15
16 65
        return $fields;
17
    }
18
19
    public function toJson(): string
20
    {
21
        return json_encode($this->toArray());
22
    }
23
24 2
    public function toArray(): array
25
    {
26 2
        $array = [];
27
28 2
        foreach ($this->all() as $field) {
29 2
            $array[] = $field->toArray();
30
        }
31
32 2
        return $array;
33
    }
34
35
    /**
36
     * @param array $locales
37
     * @param ProvidesUrl $model
38
     * @return UrlSlugFields
39
     */
40 65
    private static function initEmptyFields(array $locales, ProvidesUrl $model): self
41
    {
42 65
        $fields = new static([]);
43
44 65
        foreach ($locales as $locale) {
45 65
            $fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale)
46 65
                                                ->setBaseUrlSegment($model->baseUrlSegment($locale))
47 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

47
                                                ->/** @scrutinizer ignore-call */ prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/')
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
48 65
                                                ->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

48
                                                ->/** @scrutinizer ignore-call */ name('url-slugs[' . $locale . ']')
Loading history...
49 65
                                                ->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

49
                                                ->/** @scrutinizer ignore-call */ label($locale);
Loading history...
50
        }
51
52 65
        return $fields;
53
    }
54
55
    /**
56
     * @param ProvidesUrl $model
57
     * @param $fields
58
     */
59 65
    private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void
60
    {
61
        $records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) {
62 20
            return $record->isRedirect();
63 65
        })->sortBy('locale');
64
65 65
        foreach ($records as $record) {
66 20
            if (!isset($fields['url-slugs.'.$record->locale])) {
67
                continue;
68
            }
69
70 20
            $fields['url-slugs.'.$record->locale]
71 20
                ->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

71
                ->/** @scrutinizer ignore-call */ 
72
                  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...
72 20
                ->setBaseUrlSegment($model->baseUrlSegment($record->locale))
73 20
                ->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/');
74
        }
75 65
    }
76
}
77