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

SettingFieldsManager::saveFields()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 24
rs 9.5555
cc 5
nc 8
nop 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Settings;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Chief\Fields\Fields;
7
use Thinktomorrow\Chief\Fields\FieldManager;
8
use Thinktomorrow\Chief\Fields\RenderingFields;
9
use Thinktomorrow\Chief\Fields\Types\Field;
10
use Thinktomorrow\Chief\Fields\Types\InputField;
11
use Thinktomorrow\Chief\Fields\Types\SelectField;
12
use Thinktomorrow\Chief\Settings\Application\ChangeHomepage;
13
use Thinktomorrow\Chief\Urls\UrlHelper;
14
use Thinktomorrow\Chief\Urls\UrlRecord;
15
16
class SettingFieldsManager extends Fields implements FieldManager
17
{
18
    use RenderingFields;
0 ignored issues
show
Bug introduced by
The trait Thinktomorrow\Chief\Fields\RenderingFields requires the property $model which is not provided by Thinktomorrow\Chief\Settings\SettingFieldsManager.
Loading history...
19
20
    /** @var Settings */
21
    private $settings;
22
23
    public function __construct(Settings $settings)
24
    {
25
        $this->settings = $settings;
26
    }
27
28
    public function fields(): Fields
29
    {
30
        return new Fields([
31
            SelectField::make('homepage')
32
                ->name('homepage[:locale]')
33
                ->options(UrlHelper::allOnlineSingles())
34
                ->translatable(config('translatable.locales'))
35
                ->validation('required')
36
                ->grouped()
37
                ->label('Homepagina')
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\SelectField. 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

37
                ->/** @scrutinizer ignore-call */ label('Homepagina')
Loading history...
38
                ->description('Geef hier de homepagina voor de site op.'),
0 ignored issues
show
Bug introduced by
The method description() does not exist on Thinktomorrow\Chief\Fields\Types\SelectField. 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

38
                ->/** @scrutinizer ignore-call */ description('Geef hier de homepagina voor de site op.'),
Loading history...
39
            InputField::make('app_name')
40
                ->label('Site naam')
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\InputField. 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

40
                ->/** @scrutinizer ignore-call */ label('Site naam')
Loading history...
41
                ->description('Naam van de applicatie. Dit wordt getoond in o.a. de mail communicatie.'),
0 ignored issues
show
Bug introduced by
The method description() does not exist on Thinktomorrow\Chief\Fields\Types\InputField. 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

41
                ->/** @scrutinizer ignore-call */ description('Naam van de applicatie. Dit wordt getoond in o.a. de mail communicatie.'),
Loading history...
42
            InputField::make('contact_email')
43
                ->validation('email')
44
                ->label('Webmaster email')
45
                ->description('Het emailadres van de webmaster. Hierop ontvang je standaard alle contactnames.'),
46
            InputField::make('contact_name')
47
                ->label('Webmaster naam')
48
                ->description('Voor en achternaam van de webmaster.'),
49
        ]);
50
    }
51
52
    public function fieldValue(Field $field, $locale = null)
53
    {
54
        return $this->settings->get($field->key(), $locale);
0 ignored issues
show
Bug introduced by
The method key() does not exist on Thinktomorrow\Chief\Fields\Types\Field. 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

54
        return $this->settings->get($field->/** @scrutinizer ignore-call */ key(), $locale);
Loading history...
55
    }
56
57
    public function saveFields(Request $request)
58
    {
59
        $existingHomepageValue = [];
60
61
        foreach ($this->fields() as $key => $field) {
62
            if (!$setting = Setting::where('key', $key)->first()) {
63
                Setting::create([
64
                    'key' => $key,
65
                    'value' => $request->get($key, ''),
66
                ]);
67
68
                continue;
69
            }
70
71
            if ($key === Setting::HOMEPAGE) {
72
                $existingHomepageValue = $setting->value;
73
            }
74
75
            $setting->update(['value' => $request->get($key, '')]);
76
        }
77
78
        // A changed homepage needs to be reflected in the urls as well in order to respond to incoming requests.
79
        if ($request->filled(Setting::HOMEPAGE)) {
80
            app(ChangeHomepage::class)->onSettingChanged($existingHomepageValue);
0 ignored issues
show
Bug introduced by
It seems like $existingHomepageValue can also be of type string; however, parameter $existingValues of Thinktomorrow\Chief\Sett...age::onSettingChanged() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            app(ChangeHomepage::class)->onSettingChanged(/** @scrutinizer ignore-type */ $existingHomepageValue);
Loading history...
81
        }
82
    }
83
}
84