Passed
Push — 0.3 ( b25152...8c4a77 )
by
unknown
85:07 queued 74:51
created

SettingFieldsManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
eloc 35
c 1
b 1
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fields() 0 20 1
A fieldValue() 0 3 1
A saveFields() 0 24 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
    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
                ->grouped()
36
                ->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

36
                ->/** @scrutinizer ignore-call */ label('Homepagina')
Loading history...
37
                ->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

37
                ->/** @scrutinizer ignore-call */ description('Geef hier de homepagina voor de site op.'),
Loading history...
38
            InputField::make('app_name')
39
                ->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

39
                ->/** @scrutinizer ignore-call */ label('Site naam')
Loading history...
40
                ->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

40
                ->/** @scrutinizer ignore-call */ description('Naam van de applicatie. Dit wordt getoond in o.a. de mail communicatie.'),
Loading history...
41
            InputField::make('contact_email')
42
                ->validation('email')
43
                ->label('Webmaster email')
44
                ->description('Het emailadres van de webmaster. Hierop ontvang je standaard alle contactnames.'),
45
            InputField::make('contact_name')
46
                ->label('Webmaster naam')
47
                ->description('Voor en achternaam van de webmaster.'),
48
        ]);
49
    }
50
51
    public function fieldValue(Field $field, $locale = null)
52
    {
53
        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

53
        return $this->settings->get($field->/** @scrutinizer ignore-call */ key(), $locale);
Loading history...
54
    }
55
56
    public function saveFields(Request $request)
57
    {
58
        $existingHomepageValue = [];
59
60
        foreach ($this->fields() as $key => $field) {
61
            if (!$setting = Setting::where('key', $key)->first()) {
62
                Setting::create([
63
                    'key' => $key,
64
                    'value' => $request->get($key, ''),
65
                ]);
66
67
                continue;
68
            }
69
70
            if ($key === Setting::HOMEPAGE) {
71
                $existingHomepageValue = $setting->value;
72
            }
73
74
            $setting->update(['value' => $request->get($key, '')]);
75
        }
76
77
        // A changed homepage needs to be reflected in the urls as well in order to respond to incoming requests.
78
        if ($request->filled(Setting::HOMEPAGE)) {
79
            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

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