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

ModuleManager::saveCreateFields()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Modules;
6
7
use Illuminate\Http\Request;
8
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract;
9
use Thinktomorrow\Chief\Fields\Fields;
10
use Thinktomorrow\Chief\Fields\Types\HtmlField;
11
use Thinktomorrow\Chief\Fields\Types\InputField;
12
use Thinktomorrow\Chief\Management\AbstractManager;
13
use Thinktomorrow\Chief\Management\Details\Details;
14
use Thinktomorrow\Chief\Management\Exceptions\DeleteAborted;
15
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute;
16
use Thinktomorrow\Chief\Management\Manager;
17
use Thinktomorrow\Chief\Management\Managers;
18
use Thinktomorrow\Chief\Modules\Application\DeleteModule;
19
20
class ModuleManager extends AbstractManager implements Manager
21 39
{
22
    public function details(): Details
23 39
    {
24 39
        $modelDetails = parent::details();
25 39
        $modelDetails = $modelDetails->set('plural', $this->model->isPageSpecific() ? 'eigen modules' : 'vaste modules');
26
        $modelDetails = $modelDetails->set('title', $this->model->slug);
27 39
28
        return $modelDetails;
29
    }
30 45
31
    public function route($verb): ?string
32
    {
33
34
        /**
35
         * Page specific modules are expected to be found and managed in the context of a certain page.
36
         * Therefore the index of these modules is at the modules tab of this page model.
37 45
         */
38
        if ($verb == 'index' && $this->model->isPageSpecific()) {
39
            return app(Managers::class)->findByModel($this->model->page)->route('edit') . '#eigen-modules';
40
        }
41
42 45
        $routes = [
43 45
            'index'  => route('chief.back.modules.index', [$this->registration->key()]),
0 ignored issues
show
Bug introduced by
The method key() does not exist on Thinktomorrow\Chief\Management\Register. ( Ignorable by Annotation )

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

43
            'index'  => route('chief.back.modules.index', [$this->registration->/** @scrutinizer ignore-call */ key()]),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44 45
            'create' => route('chief.back.managers.create', [$this->registration->key()]),
45
            'store'  => route('chief.back.managers.store', [$this->registration->key()]),
46
        ];
47 45
48 30
        if (array_key_exists($verb, $routes)) {
49
            return $routes[$verb] ?? null;
50
        }
51 21
52 21
        $routes = array_merge($routes, [
53 21
            'edit'   => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]),
0 ignored issues
show
Bug introduced by
Accessing id on the interface Thinktomorrow\Chief\Management\ManagedModel suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54 21
            'update' => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]),
55 21
            'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]),
56 21
            'upload' => route('chief.back.managers.media.upload', [
57 21
                $this->registration->key(),
58
                $this->existingModel()->id,
59
            ]),
60
        ]);
61 21
62
        return $routes[$verb] ?? null;
63
    }
64 41
65
    public function can($verb): bool
66
    {
67 41
        try {
68
            $this->authorize($verb);
69 40
70 3
            return parent::can($verb);
71 3
        } catch (NotAllowedManagerRoute $e) {
72
            return false;
73
        }
74
    }
75 41
76
    private function authorize($verb)
77 41
    {
78
        $permission = 'update-page';
79 41
80 25
        if (in_array($verb, ['index', 'show'])) {
81 18
            $permission = 'view-page';
82 7
        } elseif (in_array($verb, ['create', 'store'])) {
83 14
            $permission = 'create-page';
84 2
        } elseif (in_array($verb, ['delete'])) {
85
            $permission = 'delete-page';
86
        }
87 41
88 1
        if (!auth()->guard('chief')->user()->hasPermissionTo($permission)) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

88
        if (!auth()->guard('chief')->user()->/** @scrutinizer ignore-call */ hasPermissionTo($permission)) {
Loading history...
89
            throw NotAllowedManagerRoute::notAllowedPermission($permission, $this);
90 40
        }
91
    }
92
93
    /**
94
     * The set of fields that should be manageable for a certain model.
95
     *
96
     * Additionally, you should:
97
     * 1. Make sure to setup the proper migrations and
98
     * 2. For a translatable field you should add this field to the $translatedAttributes property of the model as well.
99
     *
100
     * @return Fields
101 11
     */
102
    public function fields(): Fields
103 11
    {
104 11
        return new Fields([
105 11
            InputField::make('slug')
106 11
                ->label('Interne benaming')
107 11
                ->validation('required', ['slug' => 'Interne titel is verplicht']),
108 11
            InputField::make('title')
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

108
            /** @scrutinizer ignore-deprecated */ InputField::make('title')

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...
109 11
                ->translatable($this->model->availableLocales())
110 11
                ->label('titel'),
111 11
            HtmlField::make('content')
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

111
            /** @scrutinizer ignore-deprecated */ HtmlField::make('content')

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...
112 11
                ->translatable($this->model->availableLocales())
113
                ->label('inhoud'),
114
        ]);
115
    }
116 10
117
    public function saveCreateFields(Request $request): void
118
    {
119 10
        // Store the morph_key upon creation
120 3
        if ($this->model instanceof MorphableContract && !$this->model->morph_key) {
0 ignored issues
show
Bug introduced by
Accessing morph_key on the interface Thinktomorrow\Chief\Conc...hable\MorphableContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
121
            $this->model->morph_key = $this->model->morphKey();
122
        }
123 10
124 10
        parent::saveCreateFields($request);
125
    }
126 2
127
    public function delete()
128 2
    {
129
        $this->guard('delete');
130 2
131
        if (request()->get('deleteconfirmation') !== 'DELETE') {
132
            throw new DeleteAborted();
133
        }
134 2
135 2
        app(DeleteModule::class)->handle($this->model->id);
136
    }
137 4
138
    public function storeRequest(Request $request): Request
139 4
    {
140 4
        $trans = [];
141
        foreach ($request->input('trans', []) as $locale => $translation) {
142
            if (is_array_empty($translation)) {
143
                continue;
144
            }
145
146
            $trans[$locale] = $translation;
147
        }
148 4
149
        return $request->merge(['trans' => $trans]);
150
    }
151 8
152
    public function updateRequest(Request $request): Request
153 8
    {
154 8
        $trans = [];
155 6
        foreach ($request->input('trans', []) as $locale => $translation) {
156
            if (is_array_empty($translation)) {
157
158
                // Nullify all values
159
                $trans[$locale] = array_map(function ($value) {
160
                    return null;
161
                }, $translation);
162
                continue;
163
            }
164 6
165
            $trans[$locale] = $translation;
166
        }
167 8
168
        return $request->merge(['trans' => $trans]);
169
    }
170
}
171