Passed
Push — dependabot/npm_and_yarn/@vue/t... ( 1a0b23...9776fc )
by
unknown
84:45 queued 64:29
created

ChangeHomepage::onSettingChanged()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 22
rs 9.5555
cc 5
nc 8
nop 1
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
    public function onSettingChanged(array $existingValues)
16
    {
17
        $setting = Setting::findByKey(Setting::HOMEPAGE);
18
19
        $flatReferences = is_array($setting->value)
0 ignored issues
show
introduced by
The condition is_array($setting->value) is always false.
Loading history...
20
            ? $setting->value
21
            : array_fill_keys(config('translatable.locales'), $setting->value);
22
23
        $this->assertNoEmptyValues($flatReferences);
24
25
        foreach ($flatReferences as $locale => $flatReferenceString) {
26
27
            // If existing value has changed, we'll need to revert this previous value
28
            if (isset($existingValues[$locale])) {
29
                if ($flatReferenceString != $existingValues[$locale]) {
30
                    $flatReferenceInstance = FlatReferenceFactory::fromString(($existingValues[$locale]));
31
                    (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

31
                    (new RevertUrlSlug(/** @scrutinizer ignore-type */ $flatReferenceInstance->instance()))->handle($locale);
Loading history...
32
                }
33
            }
34
35
            $flatReferenceInstance = FlatReferenceFactory::fromString($flatReferenceString);
36
            (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

36
            (new SaveUrlSlugs(/** @scrutinizer ignore-type */ $flatReferenceInstance->instance()))->strict(false)->handle([$locale => '/']);
Loading history...
37
        }
38
    }
39
40
    public function onUrlChanged(UrlRecord $urlRecord)
41
    {
42
        $model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id);
43
44
        if (!$homepage = Setting::findByKey(Setting::HOMEPAGE)) {
45
            $homepage = Setting::create(['key' => Setting::HOMEPAGE, 'value' => []]);
46
        }
47
48
        $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

48
        $homepage->value = array_merge(/** @scrutinizer ignore-type */ $homepage->value, [$urlRecord->locale => $model->flatReference()->get()]);
Loading history...
49
        $homepage->save();
50
    }
51
52
    private function assertNoEmptyValues(array $flatReferences)
53
    {
54
        foreach ($flatReferences as $locale => $flatReferenceString) {
55
            if (!$flatReferenceString) {
56
                throw new \InvalidArgumentException('Homepage setting value cannot be empty. Value for locale ' . $locale . ' is missing.');
57
            }
58
        }
59
    }
60
}
61