Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

AbstractManager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 28
eloc 65
c 2
b 1
f 0
dl 0
loc 219
ccs 69
cts 72
cp 0.9583
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A findManaged() 0 7 1
A manage() 0 5 1
A route() 0 21 2
A fieldsWithAssistantFields() 0 13 3
A can() 0 3 1
A hasExistingModel() 0 3 2
A guard() 0 7 2
A storeRequest() 0 3 1
A validateConstraints() 0 4 2
A delete() 0 3 1
A model() 0 3 1
A updateRequest() 0 3 1
A filters() 0 3 1
A findAllManaged() 0 18 3
A fieldArrangement() 0 3 1
A fields() 0 3 1
A existingModel() 0 7 2
A requestContainsTranslations() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Management;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand;
8
use Thinktomorrow\Chief\Fields\FieldArrangement;
9
use Thinktomorrow\Chief\Fields\Fields;
10
use Thinktomorrow\Chief\Fields\RenderingFields;
11
use Thinktomorrow\Chief\Fields\SavingFields;
12
use Thinktomorrow\Chief\Filters\Filters;
13
use Thinktomorrow\Chief\Management\Assistants\AssistedManager;
14
use Thinktomorrow\Chief\Management\Details\HasDetails;
15
use Thinktomorrow\Chief\Management\Details\HasSections;
16
use Thinktomorrow\Chief\Management\Exceptions\NonExistingRecord;
17
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute;
18
19
abstract class AbstractManager
20
{
21
    use RenderingFields,
0 ignored issues
show
introduced by
The trait Thinktomorrow\Chief\Management\Details\HasDetails requires some properties which are not provided by Thinktomorrow\Chief\Management\AbstractManager: $labelPlural, $id, $labelSingular, $title
Loading history...
introduced by
The trait Thinktomorrow\Chief\Management\ManagesPagebuilder requires some properties which are not provided by Thinktomorrow\Chief\Management\AbstractManager: $trans, $slug, $page_id, $id
Loading history...
Bug introduced by
The trait Thinktomorrow\Chief\Fields\SavingFields requires the property $key which is not provided by Thinktomorrow\Chief\Management\AbstractManager.
Loading history...
22
        SavingFields,
23
        HasDetails,
24
        HasSections,
25
        ManagesMedia,
26
        ManagesPagebuilder,
27
        TranslatableCommand,
28
        AssistedManager;
29
30
    protected $translation_columns = [];
31
32
    protected $model;
33
34
    /** @var Register */
35
    protected $registration;
36
37 146
    public function __construct(Registration $registration)
38
    {
39 146
        $this->registration = $registration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $registration of type Thinktomorrow\Chief\Management\Registration is incompatible with the declared type Thinktomorrow\Chief\Management\Register of property $registration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
41
        // Upon instantiation, a general model is set that doesn't point to a persisted record.
42 146
        $this->manage(app($this->registration->model()));
43
44
        // Check if key and model are present since the model should be set by the manager itself
45 146
        $this->validateConstraints();
46 146
    }
47
48 146
    public function manage($model): Manager
49
    {
50 146
        $this->model = $model;
51
52 146
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Thinktomorrow\Chief\Management\AbstractManager which is incompatible with the type-hinted return Thinktomorrow\Chief\Management\Manager.
Loading history...
53
    }
54
55 84
    public function findManaged($id): Manager
56
    {
57 84
        $model = $this->registration->model();
0 ignored issues
show
Bug introduced by
The method model() 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

57
        /** @scrutinizer ignore-call */ 
58
        $model = $this->registration->model();

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...
58
59 84
        $modelInstance = $model::where('id', $id)->withoutGlobalScopes()->first();
60
61 84
        return (new static($this->registration))->manage($modelInstance);
0 ignored issues
show
Bug introduced by
$this->registration of type Thinktomorrow\Chief\Management\Register is incompatible with the type Thinktomorrow\Chief\Management\Registration expected by parameter $registration of Thinktomorrow\Chief\Mana...tManager::__construct(). ( Ignorable by Annotation )

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

61
        return (new static(/** @scrutinizer ignore-type */ $this->registration))->manage($modelInstance);
Loading history...
62
    }
63
64 1
    public function findAllManaged($apply_filters = false): Collection
65
    {
66 1
        $model = $this->registration->model();
67
68 1
        $builder = (new $model)->query();
69
70 1
        if ($apply_filters) {
71 1
            $this->filters()->apply($builder);
72
        }
73
74 1
        if ($this->isAssistedBy('publish')) {
75 1
            $builder->orderBy('published', 'DESC');
76
        }
77
78 1
        $builder->orderBy('updated_at', 'DESC');
79
80
        return $builder->get()->map(function ($model) {
81 1
            return (new static($this->registration))->manage($model);
0 ignored issues
show
Bug introduced by
$this->registration of type Thinktomorrow\Chief\Management\Register is incompatible with the type Thinktomorrow\Chief\Management\Registration expected by parameter $registration of Thinktomorrow\Chief\Mana...tManager::__construct(). ( Ignorable by Annotation )

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

81
            return (new static(/** @scrutinizer ignore-type */ $this->registration))->manage($model);
Loading history...
82 1
        });
83
    }
84
85 88
    public function model()
86
    {
87 88
        return $this->model;
88
    }
89
90 92
    public function hasExistingModel(): bool
91
    {
92 92
        return ($this->model && $this->model->exists);
93
    }
94
95
    /**
96
     * If the model exists return it otherwise
97
     * throws a nonExistingRecord exception;
98
     *
99
     * @throws NonExistingRecord
100
     */
101 92
    protected function existingModel()
102
    {
103 92
        if (!$this->hasExistingModel()) {
104 1
            throw new NonExistingRecord('Model does not exist yet but is expected.');
105
        }
106
107 91
        return $this->model;
108
    }
109
110
    /**
111
     * Determine which actions should be available for this
112
     * manager and their respective routed urls.
113
     *
114
     * @param $verb
115
     * @return null|string
116
     * @throws NonExistingRecord
117
     */
118 115
    public function route($verb): ?string
119
    {
120
        $routes = [
121 115
            'index'   => route('chief.back.managers.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

121
            'index'   => route('chief.back.managers.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...
122 115
            'create'  => route('chief.back.managers.create', [$this->registration->key()]),
123 115
            'store'   => route('chief.back.managers.store', [$this->registration->key()]),
124
        ];
125
126 115
        if (array_key_exists($verb, $routes)) {
127 66
            return $routes[$verb] ?? null;
128
        }
129
130
        //These routes expect the model to be persisted in the database
131
        $modelRoutes = [
132 92
            'edit'    => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]),
133 91
            'update'  => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]),
134 91
            'delete'  => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]),
135 91
            'upload'  => route('chief.back.managers.media.upload', [$this->registration->key(), $this->existingModel()->id]),
136
        ];
137
138 91
        return $modelRoutes[$verb] ?? null;
139
    }
140
141 118
    public function can($verb): bool
142
    {
143 118
        return !is_null($this->route($verb));
144
    }
145
146 104
    public function guard($verb): Manager
147
    {
148 104
        if (! $this->can($verb)) {
149 1
            NotAllowedManagerRoute::notAllowedVerb($verb, $this);
150
        }
151
152 103
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Thinktomorrow\Chief\Management\AbstractManager which is incompatible with the type-hinted return Thinktomorrow\Chief\Management\Manager.
Loading history...
153
    }
154
155 81
    public function fields(): Fields
156
    {
157 81
        return new Fields();
158
    }
159
160
    /**
161
     * Enrich the manager fields with any of the assistant specified fields
162
     *
163
     * @return Fields
164
     * @throws \Exception
165
     */
166 98
    public function fieldsWithAssistantFields(): Fields
167
    {
168 98
        $fields = $this->fields();
169
170 98
        foreach ($this->assistants() as $assistant) {
171 75
            if (! method_exists($assistant, 'fields')) {
172 73
                continue;
173
            }
174
175 75
            $fields = $fields->merge($assistant->fields());
176
        }
177
178 98
        return $fields;
179
    }
180
181
    /**
182
     * This determines the arrangement of the manageable fields
183
     * on the create and edit forms. By default, all fields
184
     * are presented in their order of appearance
185
     *
186
     * @param null $key pinpoint to a specific field arrangement e.g. for create page.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
187
     * @return FieldArrangement
188
     * @throws \Exception
189
     */
190 3
    public function fieldArrangement($key = null): FieldArrangement
191
    {
192 3
        return new FieldArrangement($this->fieldsWithAssistantFields());
193
    }
194
195 1
    public function delete()
196
    {
197 1
        $this->model->delete();
0 ignored issues
show
Bug introduced by
The method delete() 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

197
        $this->model->/** @scrutinizer ignore-call */ 
198
                      delete();

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...
198 1
    }
199
200
    public static function filters(): Filters
201
    {
202
        return new Filters();
203
    }
204
205
    /**
206
     * This method can be used to manipulate the store request payload
207
     * before being passed to the storing / updating the models.
208
     *
209
     * @param Request $request
210
     * @return Request
211
     */
212 23
    public function storeRequest(Request $request): Request
213
    {
214 23
        return $request;
215
    }
216
217
    /**
218
     * This method can be used to manipulate the update request payload
219
     * before being passed to the storing / updating the models.
220
     *
221
     * @param Request $request
222
     * @return Request
223
     */
224 22
    public function updateRequest(Request $request): Request
225
    {
226 22
        return $request;
227
    }
228
229 75
    protected function requestContainsTranslations(Request $request): bool
230
    {
231 75
        return $request->has('trans');
232
    }
233
234 146
    protected function validateConstraints()
235
    {
236 146
        if (!$this->model) {
237
            throw new \DomainException('Model class should be set for this manager. Please set the model property default via the constructor or by extending the setupDefaults method.');
238
        }
239 146
    }
240
}
241