Passed
Push — master ( f2fe9c...c37bd3 )
by Philippe
02:30 queued 12s
created

ModuleManager::route()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

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

40
            '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...
41 43
            'create'  => route('chief.back.managers.create', [$this->registration->key()]),
42 43
            'store'   => route('chief.back.managers.store', [$this->registration->key()]),
43
        ];
44
45 43
        if (array_key_exists($verb, $routes)) {
46 29
            return $routes[$verb] ?? null;
47
        }
48
49 20
        $routes = array_merge($routes, [
50 20
            'edit'    => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]),
51 20
            'update'  => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]),
52 20
            'delete'  => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]),
53 20
            'upload'  => route('chief.back.managers.media.upload', [$this->registration->key(), $this->existingModel()->id])
54
        ]);
55
56 20
        return $routes[$verb] ?? null;
57
    }
58
59 39
    public function can($verb): bool
60
    {
61
        try {
62 39
            $this->authorize($verb);
63
64 38
            return parent::can($verb);
65 3
        } catch (NotAllowedManagerRoute $e) {
66 3
            return false;
67
        }
68
    }
69
70 39
    private function authorize($verb)
71
    {
72 39
        $permission = 'update-page';
73
74 39
        if (in_array($verb, ['index','show'])) {
75 24
            $permission = 'view-page';
76 17
        } elseif (in_array($verb, ['create','store'])) {
77 7
            $permission = 'create-page';
78 13
        } elseif (in_array($verb, ['delete'])) {
79 3
            $permission = 'delete-page';
80
        }
81
82 39
        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

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

100
                ->/** @scrutinizer ignore-call */ label('Interne benaming')
Loading history...
101 11
                ->validation('required', ['slug' => 'Interne titel is verplicht']),
102 11
            InputField::make('title')
103 11
                ->translatable($this->model->availableLocales())
104 11
                ->label('titel'),
105 11
            HtmlField::make('content')
106 11
                ->translatable($this->model->availableLocales())
107 11
                ->label('inhoud'),
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\HtmlField. 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

107
                ->/** @scrutinizer ignore-call */ label('inhoud'),
Loading history...
108
        ]);
109
    }
110
111 9
    public function saveFields(Request $request)
112
    {
113
        // Store the morph_key upon creation
114 9
        if (! $this->model->morph_key) {
115 3
            $this->model->morph_key = $this->model->morphKey();
116
        }
117
118 9
        parent::saveFields($request);
119 9
    }
120
121 2
    public function delete()
122
    {
123 2
        $this->guard('delete');
124
125 2
        if (request()->get('deleteconfirmation') !== 'DELETE') {
126
            throw new DeleteAborted();
127
        }
128
129 2
        app(DeleteModule::class)->handle($this->model->id);
130 2
    }
131
132 4
    public function storeRequest(Request $request): Request
133
    {
134 4
        $trans = [];
135 4
        foreach ($request->get('trans', []) as $locale => $translation) {
136
            if (is_array_empty($translation)) {
137
                continue;
138
            }
139
140
            $trans[$locale] = $translation;
141
        }
142
143 4
        return $request->merge(['trans' => $trans]);
144
    }
145
146 7
    public function updateRequest(Request $request): Request
147
    {
148 7
        $trans = [];
149 7
        foreach ($request->get('trans', []) as $locale => $translation) {
150 6
            if (is_array_empty($translation)) {
151
152
                // Nullify all values
153
                $trans[$locale] = array_map(function ($value) {
154
                    return null;
155
                }, $translation);
156
                continue;
157
            }
158
159 6
            $trans[$locale] = $translation;
160
        }
161
162 7
        return $request->merge(['trans' => $trans]);
163
    }
164
}
165