Passed
Push — master ( 970265...da43ab )
by Philippe
64:51 queued 33:01
created

ModuleManager::authorize()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 8
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
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 25
    public function details(): Details
20
    {
21 25
        $modelDetails = parent::details();
22 25
        $modelDetails = $modelDetails->set('plural', $this->model->isPageSpecific() ? 'eigen modules' : 'vaste modules');
0 ignored issues
show
Bug introduced by
The method isPageSpecific() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

22
        $modelDetails = $modelDetails->set('plural', $this->model->/** @scrutinizer ignore-call */ isPageSpecific() ? 'eigen modules' : 'vaste modules');

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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
23 25
        $modelDetails = $modelDetails->set('title', $this->model->slug);
24
25 25
        return $modelDetails;
26
    }
27
28 26
    public function route($verb): ?string
29
    {
30
        /**
31
         * Page specific modules are expected to be found and managed in the context of a certain page.
32
         * Therefore the index of these modules is at the modules tab of this page model.
33
         */
34 26
        if ($verb == 'index' && $this->model->isPageSpecific()) {
35
            return app(Managers::class)->findByModel($this->model->page)->route('edit').'#eigen-modules';
36
        }
37
38
        $routes = [
39 26
            'index'   => route('chief.back.modules.index'),
40 26
            'create'  => route('chief.back.managers.create', [$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
            'create'  => route('chief.back.managers.create', [$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 26
            'store'   => route('chief.back.managers.store', [$this->registration->key(), $this->model->id]),
42 26
            'edit'    => route('chief.back.managers.edit', [$this->registration->key(), $this->model->id]),
43 26
            'update'  => route('chief.back.managers.update', [$this->registration->key(), $this->model->id]),
44 26
            'delete'  => route('chief.back.managers.delete', [$this->registration->key(), $this->model->id]),
45 26
            'upload'  => route('chief.back.managers.media.upload', [$this->registration->key(), $this->model->id])
46
        ];
47
48 26
        return $routes[$verb] ?? null;
49
    }
50
51 27
    public function can($verb): bool
52
    {
53
        try {
54 27
            $this->authorize($verb);
55 1
        } catch (NotAllowedManagerRoute $e) {
56 1
            return false;
57
        }
58
59 26
        return parent::can($verb);
60
    }
61
62 27
    private function authorize($verb)
63
    {
64 27
        $permission = 'update-page';
65
66 27
        if (in_array($verb, ['index','show'])) {
67 17
            $permission = 'view-page';
68 11
        } elseif (in_array($verb, ['create','store'])) {
69 3
            $permission = 'create-page';
70 8
        } elseif (in_array($verb, ['delete'])) {
71 3
            $permission = 'delete-page';
72
        }
73
74 27
        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

74
        if (! auth()->guard('chief')->user()->/** @scrutinizer ignore-call */ hasPermissionTo($permission)) {
Loading history...
75 1
            throw NotAllowedManagerRoute::notAllowedPermission($permission, $this);
76
        }
77 26
    }
78
79
    /**
80
     * The set of fields that should be manageable for a certain model.
81
     *
82
     * Additionally, you should:
83
     * 1. Make sure to setup the proper migrations and
84
     * 2. For a translatable field you should add this field to the $translatedAttributes property of the model as well.
85
     *
86
     * @return Fields
87
     */
88 9
    public function fields(): Fields
89
    {
90 9
        return new Fields([
91 9
            InputField::make('slug')
92 9
                ->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

92
                ->/** @scrutinizer ignore-call */ label('Interne benaming')
Loading history...
93 9
                ->validation('required', ['slug' => 'Interne titel is verplicht']),
94 9
            InputField::make('title')
95 9
                ->translatable($this->model->availableLocales())
0 ignored issues
show
Bug introduced by
The method availableLocales() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

95
                ->translatable($this->model->/** @scrutinizer ignore-call */ availableLocales())

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...
96 9
                ->label('titel'),
97 9
            HtmlField::make('content')
98 9
                ->translatable($this->model->availableLocales())
99 9
                ->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

99
                ->/** @scrutinizer ignore-call */ label('inhoud'),
Loading history...
100
        ]);
101
    }
102
103 6
    public function saveFields(): Manager
104
    {
105
        // Store the morph_key upon creation
106 6
        if (! $this->model->morph_key) {
107 2
            $this->model->morph_key = $this->model->morphKey();
0 ignored issues
show
Bug introduced by
The method morphKey() does not exist on Illuminate\Contracts\Foundation\Application. ( 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 */ 
108
            $this->model->morph_key = $this->model->morphKey();

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...
108
        }
109
110 6
        return parent::saveFields();
111
    }
112
113 2
    public function delete()
114
    {
115 2
        $this->guard('delete');
116
117 2
        if (request()->get('deleteconfirmation') !== 'DELETE') {
118
            throw new DeleteAborted();
119
        }
120
121 2
        app(DeleteModule::class)->handle($this->model->id);
122 2
    }
123
124 3
    public function storeRequest(Request $request): Request
125
    {
126 3
        $trans = [];
127 3
        foreach ($request->get('trans', []) as $locale => $translation) {
128
            if (is_array_empty($translation)) {
129
                continue;
130
            }
131
132
            $trans[$locale] = $translation;
133
        }
134
135 3
        return $request->merge(['trans' => $trans]);
136
    }
137
138 5
    public function updateRequest(Request $request): Request
139
    {
140 5
        $trans = [];
141 5
        foreach ($request->get('trans', []) as $locale => $translation) {
142 5
            if (is_array_empty($translation)) {
143
144
                // Nullify all values
145
                $trans[$locale] = array_map(function ($value) {
146
                    return null;
147
                }, $translation);
148
                continue;
149
            }
150
151 5
            $trans[$locale] = $translation;
152
        }
153
154 5
        return $request->merge(['trans' => $trans]);
155
    }
156
}
157