Passed
Push — 0.3 ( b25152...8c4a77 )
by
unknown
85:07 queued 74:51
created

AbstractManager::getFieldValue()   B

Complexity

Conditions 8
Paths 22

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 16
c 1
b 1
f 0
dl 0
loc 36
ccs 16
cts 17
cp 0.9412
rs 8.4444
cc 8
nc 22
nop 2
crap 8.013

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractManager::updateRequest() 0 3 1
A AbstractManager::storeRequest() 0 3 1
A AbstractManager::delete() 0 3 1
A AbstractManager::filters() 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 136
37
    public function __construct(Registration $registration)
38 136
    {
39
        $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 136
        // Upon instantiation, a general model is set that doesn't point to a persisted record.
42
        $this->manage(app($this->registration->model()));
43
44 136
        // Check if key and model are present since the model should be set by the manager itself
45 136
        $this->validateConstraints();
46
    }
47 136
48
    public function manage($model): Manager
49 136
    {
50
        $this->model = $model;
51 136
52
        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 80
55
    public function findManaged($id): Manager
56 80
    {
57
        $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 80
59
        $modelInstance = $model::where('id', $id)->withoutGlobalScopes()->first();
60 80
61
        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 1
64
    public function findAllManaged($apply_filters = false): Collection
65 1
    {
66
        $model = $this->registration->model();
67 1
68 1
        if ($apply_filters) {
69 1
            $builder = (new $model)->query();
70
            $this->filters()->apply($builder);
71 1
72
            $results = $builder->get();
73
        } else {
74
            $results = $model::all();
75
        }
76
77 1
        return $results->map(function ($model) {
78 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

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

118
            '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...
119 107
            'create'  => route('chief.back.managers.create', [$this->registration->key()]),
120
            'store'   => route('chief.back.managers.store', [$this->registration->key()]),
121
        ];
122 107
123 62
        if (array_key_exists($verb, $routes)) {
124
            return $routes[$verb] ?? null;
125
        }
126
127
        //These routes expect the model to be persisted in the database
128 84
        $modelRoutes = [
129 83
            'edit'    => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]),
130 83
            'update'  => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]),
131 83
            'delete'  => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]),
132
            'upload'  => route('chief.back.managers.media.upload', [$this->registration->key(), $this->existingModel()->id]),
133
        ];
134 83
135
        return $modelRoutes[$verb] ?? null;
136
    }
137 110
138
    public function can($verb): bool
139 110
    {
140
        return !is_null($this->route($verb));
141
    }
142 96
143
    public function guard($verb): Manager
144 96
    {
145 1
        if (! $this->can($verb)) {
146
            NotAllowedManagerRoute::notAllowedVerb($verb, $this);
147
        }
148 95
149
        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...
150
    }
151 74
152
    public function fields(): Fields
153 74
    {
154
        return new Fields();
155
    }
156
157
    /**
158
     * Enrich the manager fields with any of the assistant specified fields
159
     *
160
     * @return Fields
161
     * @throws \Exception
162 91
     */
163
    public function fieldsWithAssistantFields(): Fields
164 91
    {
165
        $fields = $this->fields();
166 91
167 67
        foreach ($this->assistants() as $assistant) {
168 65
            if (! method_exists($assistant, 'fields')) {
169
                continue;
170
            }
171 67
172
            $fields = $fields->merge($assistant->fields());
173
        }
174 91
175
        return $fields;
176
    }
177
178
    /**
179
     * This determines the arrangement of the manageable fields
180
     * on the create and edit forms. By default, all fields
181
     * are presented in their order of appearance
182
     *
183
     * @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...
184
     * @return FieldArrangement
185
     * @throws \Exception
186 3
     */
187
    public function fieldArrangement($key = null): FieldArrangement
188 3
    {
189
        return new FieldArrangement($this->fieldsWithAssistantFields());
190
    }
191 5
192
    public function delete()
193
    {
194 5
        $this->model->delete();
195 5
    }
196 5
197 3
    public static function filters(): Filters
198 5
    {
199
        return new Filters();
200
    }
201
202 5
    /**
203
     * This method can be used to manipulate the store request payload
204
     * before being passed to the storing / updating the models.
205 4
     *
206 4
     * @param Request $request
207 4
     * @return Request
208
     */
209 4
    public function storeRequest(Request $request): Request
210
    {
211
        return $request;
212
    }
213
214
    /**
215
     * This method can be used to manipulate the update request payload
216
     * before being passed to the storing / updating the models.
217
     *
218 4
     * @param Request $request
219 3
     * @return Request
220
     */
221
    public function updateRequest(Request $request): Request
222 2
    {
223 1
        return $request;
224
    }
225
226 2
    protected function requestContainsTranslations(Request $request): bool
227
    {
228
        return $request->has('trans');
229 4
    }
230
231 4
    protected function validateConstraints()
232
    {
233
        if (!$this->model) {
234 70
            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.');
235
        }
236
    }
237
}
238