Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

SettingFieldsManager::saveFields()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 9.5555
cc 5
nc 8
nop 1
crap 5
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 6
    public function __construct(Settings $settings)
24
    {
25 6
        $this->settings = $settings;
26 6
    }
27
28 6
    public function fields(): Fields
29
    {
30 6
        return new Fields([
31 6
            SelectField::make('homepage')
32 6
                ->name('homepage[:locale]')
33 6
                ->options(UrlHelper::allOnlineSingles())
34 6
                ->translatable(config('translatable.locales'))
35 6
                ->validation('required')
36 6
                ->grouped()
37 6
                ->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 6
                ->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 6
            InputField::make('app_name')
40 6
                ->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 6
                ->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 6
            InputField::make('contact_email')
43 6
                ->validation('email')
44 6
                ->label('Webmaster email')
45 6
                ->description('Het emailadres van de webmaster. Hierop ontvang je standaard alle contactnames.'),
46 6
            InputField::make('contact_name')
47 6
                ->label('Webmaster naam')
48 6
                ->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 5
    public function saveFields(Request $request)
58
    {
59 5
        $existingHomepageValue = [];
60
61 5
        foreach ($this->fields() as $key => $field) {
62 5
            if (!$setting = Setting::where('key', $key)->first()) {
63 5
                Setting::create([
64 5
                    'key' => $key,
65 5
                    'value' => $request->get($key, ''),
66
                ]);
67
68 5
                continue;
69
            }
70
71 2
            if ($key === Setting::HOMEPAGE) {
72 1
                $existingHomepageValue = $setting->value;
73
            }
74
75 2
            $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 5
        if ($request->filled(Setting::HOMEPAGE)) {
80 5
            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 5
    }
83
}
84