Passed
Push — ft/urls ( e40d31...cdb8a8 )
by Ben
37:03
created

UrlSlugFields::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
use Thinktomorrow\Chief\Management\Assistants\UrlAssistant;
8
9
class UrlSlugFields extends Fields
10
{
11 70
    public static function fromModel(ProvidesUrl $model)
12
    {
13 70
        $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

13
        $fields = self::initEmptyFields($model->/** @scrutinizer ignore-call */ availableLocales(), $model);
Loading history...
14
15 70
        self::fillWithExistingValues($model, $fields);
16
17 70
        return $fields;
18
    }
19
20
    public function toJson(): string
21
    {
22
        return json_encode($this->toArray());
23
    }
24
25 2
    public function toArray(): array
26
    {
27 2
        $array = [];
28
29 2
        foreach($this->all() as $field) {
30 2
            $array[] = $field->toArray();
31
        }
32
33 2
        return $array;
34
    }
35
36
    /**
37
     * @param array $locales
38
     * @param ProvidesUrl $model
39
     * @return UrlSlugFields
40
     */
41 70
    private static function initEmptyFields(array $locales, ProvidesUrl $model): self
42
    {
43
        // Add wildcard field as default
44 70
        $fields = new static([ $wildCardField = UrlSlugField::make('url-slugs.' . UrlAssistant::WILDCARD)
45 70
            ->name('url-slugs[' . UrlAssistant::WILDCARD . ']')
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

45
            ->/** @scrutinizer ignore-call */ name('url-slugs[' . UrlAssistant::WILDCARD . ']')
Loading history...
46 70
            ->label('')
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

46
            ->/** @scrutinizer ignore-call */ label('')
Loading history...
47
        ]);
48
49 70
        if (count($locales) < 2) return $fields;
50
51
        // Add description to wildcard field only when there are locale values.
52 70
        $wildCardField->label('Default link')
53 70
                      ->description('Standaard link die altijd van toepassing is indien er geen taalspecifieke link voorhanden is. Laat deze leeg indien je deze link in bepaalde talen niet wilt beschikbaar maken.');
0 ignored issues
show
Bug introduced by
The method description() 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

53
                      ->/** @scrutinizer ignore-call */ description('Standaard link die altijd van toepassing is indien er geen taalspecifieke link voorhanden is. Laat deze leeg indien je deze link in bepaalde talen niet wilt beschikbaar maken.');
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 215 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...
54
55 70
        foreach ($locales as $locale) {
56 70
            $fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale)
57 70
                                                ->setBaseUrlSegment($model->baseUrlSegment($locale))
58 70
                                                ->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

58
                                                ->/** @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...
59 70
                                                ->name('url-slugs[' . $locale . ']')
60 70
                                                ->label($locale . ' link');
61
        }
62
63 70
        return $fields;
64
    }
65
66
    /**
67
     * @param ProvidesUrl $model
68
     * @param $fields
69
     */
70 70
    private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void
71
    {
72
        $records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) {
73 21
            return $record->isRedirect();
74 70
        })->sortBy('locale');
75
76 70
        foreach ($records as $record) {
77
78 21
            if ($record->isManagedAsWildCard()) {
79 4
                $fields['url-slugs.' . UrlAssistant::WILDCARD]
80 4
                    ->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

80
                    ->/** @scrutinizer ignore-call */ 
81
                      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...
81 4
                    ->setBaseUrlSegment($model->baseUrlSegment($record->locale));
82 4
                continue;
83
            }
84
85 19
            if(!isset($fields['url-slugs.'.$record->locale])) continue;
86
87 19
            $fields['url-slugs.'.$record->locale]
88 19
                ->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

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