Passed
Push — ft/fields-refactor ( 34e13a...220f3b )
by Ben
81:47
created

ChangeHomepage::onSettingChanged()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 9.5555
c 1
b 0
f 0
cc 5
nc 8
nop 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Settings\Application;
5
6
use Thinktomorrow\Chief\Concerns\Morphable\Morphables;
7
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory;
8
use Thinktomorrow\Chief\Settings\Setting;
9
use Thinktomorrow\Chief\Urls\Application\RevertUrlSlug;
10
use Thinktomorrow\Chief\Urls\Application\SaveUrlSlugs;
11
use Thinktomorrow\Chief\Urls\UrlRecord;
12
13
class ChangeHomepage
14
{
15 4
    public function onSettingChanged(array $existingValues)
16
    {
17 4
        $setting = Setting::findByKey(Setting::HOMEPAGE);
18
19 4
        $flatReferences = is_array($setting->value)
0 ignored issues
show
introduced by
The condition is_array($setting->value) is always false.
Loading history...
20 2
            ? $setting->value
21 4
            : array_fill_keys(config('translatable.locales'), $setting->value);
22
23
        // Filter out empty values
24
//        foreach($flatReferences as $locale => $flatReferenceString)
25
//        {
26
//            if(!$flatReferenceString) {
27
//                unset($flatReferences[$locale]);
28
//            }
29
//        }
30
31 4
        foreach($flatReferences as $locale => $flatReferenceString) {
32
33 4
            if(!$flatReferenceString) {
34
35
// TODO: when empty we'll remove the entry, we'll also want to revert to last redirect.
36
37 2
                if(isset($existingValues[$locale])) {
38 1
                    $flatReferenceInstance = FlatReferenceFactory::fromString(($existingValues[$locale]));
39 1
                    (new RevertUrlSlug($flatReferenceInstance->instance()))->handle($locale);
0 ignored issues
show
Bug introduced by
$flatReferenceInstance->instance() of type Illuminate\Database\Eloquent\Model is incompatible with the type Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl expected by parameter $model of Thinktomorrow\Chief\Urls...tUrlSlug::__construct(). ( Ignorable by Annotation )

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

39
                    (new RevertUrlSlug(/** @scrutinizer ignore-type */ $flatReferenceInstance->instance()))->handle($locale);
Loading history...
40
41 1
                    continue;
42
                }
43
44
                // Find the previous value so can alter it...
45
            }
46
47 4
            $flatReferenceInstance = FlatReferenceFactory::fromString($flatReferenceString);
48 4
            (new SaveUrlSlugs($flatReferenceInstance->instance()))->strict(false)->handle([$locale => '/']);
0 ignored issues
show
Bug introduced by
$flatReferenceInstance->instance() of type Illuminate\Database\Eloquent\Model is incompatible with the type Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl expected by parameter $model of Thinktomorrow\Chief\Urls...UrlSlugs::__construct(). ( Ignorable by Annotation )

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

48
            (new SaveUrlSlugs(/** @scrutinizer ignore-type */ $flatReferenceInstance->instance()))->strict(false)->handle([$locale => '/']);
Loading history...
49
        }
50 3
    }
51
52 2
    public function onUrlChanged(UrlRecord $urlRecord)
53
    {
54 2
        $model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id);
55
56 2
        if(!$homepage = Setting::findByKey(Setting::HOMEPAGE)){
57 2
            $homepage = Setting::create(['key' => Setting::HOMEPAGE, 'value' => []]);
58
        }
59
60 2
        $homepage->value = array_merge($homepage->value, [$urlRecord->locale => $model->flatReference()->get()]);
0 ignored issues
show
Bug introduced by
$homepage->value of type string is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

60
        $homepage->value = array_merge(/** @scrutinizer ignore-type */ $homepage->value, [$urlRecord->locale => $model->flatReference()->get()]);
Loading history...
61 2
        $homepage->save();
62
    }
63
}