Passed
Push — master ( 3e205a...970265 )
by Philippe
66:06
created

ModuleManager::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
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, ['archive'])) {
71
            $permission = 'archive-page';
72 8
        } elseif (in_array($verb, ['delete'])) {
73 3
            $permission = 'delete-page';
74
        }
75
76 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

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

94
                ->/** @scrutinizer ignore-call */ label('Interne benaming')
Loading history...
95 9
                ->validation('required', ['slug' => 'Interne titel is verplicht']),
96 9
            InputField::make('title')
97 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

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

101
                ->/** @scrutinizer ignore-call */ label('inhoud'),
Loading history...
102
        ]);
103
    }
104
105 6
    public function saveFields(): Manager
106
    {
107
        // Store the morph_key upon creation
108 6
        if (! $this->model->morph_key) {
109 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

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