Passed
Push — ft/pagefield ( db2ad6...f2722b )
by Ben
10:27
created

SettingFieldsManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
eloc 41
c 4
b 1
f 0
dl 0
loc 94
ccs 39
cts 41
cp 0.9512
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A saveEditFields() 0 3 1
A createFields() 0 3 1
A __construct() 0 3 1
A editFields() 0 5 1
A saveCreateFields() 0 2 1
A fields() 0 23 1
A saveFields() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Settings;
6
7
use Illuminate\Http\Request;
8
use Thinktomorrow\Chief\Fields\Fields;
9
use Thinktomorrow\Chief\Fields\Types\Field;
10
use Thinktomorrow\Chief\Fields\FieldManager;
11
use Thinktomorrow\Chief\Fields\Types\InputField;
12
use Thinktomorrow\Chief\Fields\Types\SelectField;
13
use Thinktomorrow\Chief\Settings\Application\ChangeHomepage;
14
use Thinktomorrow\Chief\Urls\UrlHelper;
15
16
class SettingFieldsManager implements FieldManager
17
{
18
    /** @var Settings */
19
    private $settings;
20
21
    public function __construct(Settings $settings)
22
    {
23
        $this->settings = $settings;
24 6
    }
25
26 6
    public function fields(): Fields
27 6
    {
28
        return new Fields([
29 6
            SelectField::make('homepage')
0 ignored issues
show
Deprecated Code introduced by
The function Thinktomorrow\Chief\Fiel...ctField::translatable() has been deprecated: use locales(array $locales) instead ( Ignorable by Annotation )

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

29
            /** @scrutinizer ignore-deprecated */ SelectField::make('homepage')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
30
                ->name('homepage.:locale')
31 6
                ->options(UrlHelper::allOnlineModels())
32 6
                ->translatable(config('translatable.locales'))
33 6
                ->validation('required')
34 6
                ->grouped()
35 6
                ->label('Homepagina')
36 6
                ->description('Geef hier de homepagina voor de site op.'),
37 6
            InputField::make('app_name')
38 6
                ->label('Site naam')
39 6
                ->validation('required')
40 6
                ->description('Naam van de applicatie. Dit wordt getoond in o.a. de mail communicatie.'),
41 6
            InputField::make('contact_email')
42 6
                ->validation('required|email')
43 6
                ->label('Webmaster email')
44 6
                ->description('Het emailadres van de webmaster. Hierop ontvang je standaard alle contactnames.'),
45 6
            InputField::make('contact_name')
46 6
                ->validation('required')
47 6
                ->label('Webmaster naam')
48 6
                ->description('Voor en achternaam van de webmaster.'),
49 6
        ]);
50 6
    }
51 6
52
    public function editFields(): Fields
53
    {
54
        return $this->fields()->map(function (Field $field) {
55
            return $field->valueResolver(function ($model = null, $locale = null, $field) {
0 ignored issues
show
Bug introduced by
The method valueResolver() does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Fields\Types\Field. ( Ignorable by Annotation )

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

55
            return $field->/** @scrutinizer ignore-call */ valueResolver(function ($model = null, $locale = null, $field) {
Loading history...
56
                return $this->settings->get($field->getKey(), $locale);
57
            });
58
        });
59
    }
60 5
61
    public function createFields(): Fields
62 5
    {
63
        return new Fields();
64 5
    }
65 5
66 5
    /**
67 5
     * Triggers the create save action for all prepared field values.
68 5
     *
69
     * @param Request $request
70
     */
71 5
    public function saveCreateFields(Request $request): void
72
    {
73
        // Not used for settings manager but required by interface
74 2
    }
75 1
76
    /**
77
     * Triggers the edit save action for all prepared field values.
78 2
     *
79
     * @param Request $request
80
     */
81
    public function saveEditFields(Request $request): void
82 5
    {
83 5
        $this->saveFields($request);
84
    }
85 5
86
    private function saveFields(Request $request)
87
    {
88
        $existingHomepageValue = [];
89
90
        foreach ($this->fields() as $key => $field) {
91
            if (!$setting = Setting::where('key', $key)->first()) {
92
                Setting::create([
93
                    'key'   => $key,
94
                    'value' => $request->input($key, ''),
95
                ]);
96
97
                continue;
98
            }
99
100
            if ($key === Setting::HOMEPAGE && is_array($setting->value)) {
101
                $existingHomepageValue = $setting->value;
102
            }
103
104
            $setting->update(['value' => $request->input($key, '')]);
105
        }
106
107
        // A changed homepage needs to be reflected in the urls as well in order to respond to incoming requests.
108
        if ($request->filled(Setting::HOMEPAGE)) {
109
            app(ChangeHomepage::class)->onSettingChanged($existingHomepageValue);
110
        }
111
    }
112
}
113