| Total Complexity | 50 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 0 |
Complex classes like AbstractManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class AbstractManager |
||
| 27 | { |
||
| 28 | use SavingFields; |
||
| 29 | use HasDetails; |
||
|
|
|||
| 30 | use HasDetailSections; |
||
| 31 | use ManagesMedia; |
||
| 32 | use ManagesPagebuilder; |
||
| 33 | use TranslatableCommand; |
||
| 34 | use ManagesAssistants; |
||
| 35 | use ManagesFragments; |
||
| 36 | |||
| 37 | protected $translation_columns = []; |
||
| 38 | |||
| 39 | protected $model; |
||
| 40 | |||
| 41 | /** @var Register */ |
||
| 42 | protected $registration; |
||
| 43 | |||
| 44 | protected $pageCount = 20; |
||
| 45 | protected $paginated = true; |
||
| 46 | protected static $bootedTraitMethods = []; |
||
| 47 | |||
| 48 | final public function __construct(Registration $registration) |
||
| 49 | { |
||
| 50 | $this->registration = $registration; |
||
| 51 | |||
| 52 | // Upon instantiation, a general model is set that doesn't point to a persisted record. |
||
| 53 | $this->manage(app($this->registration->model())); |
||
| 54 | |||
| 55 | // Check if key and model are present since the model should be set by the manager itself |
||
| 56 | $this->validateConstraints(); |
||
| 57 | |||
| 58 | static::bootTraitMethods(); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function managerKey(): string |
||
| 64 | } |
||
| 65 | |||
| 66 | public function manage($model): Manager |
||
| 67 | { |
||
| 68 | $this->model = $model; |
||
| 69 | |||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function findManaged($id): Manager |
||
| 74 | { |
||
| 75 | $modelInstance = $this->modelInstance()::where('id', $id)->withoutGlobalScopes()->first(); |
||
| 76 | |||
| 77 | return (new static($this->registration))->manage($modelInstance); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function indexCollection() |
||
| 81 | { |
||
| 82 | $builder = ($this->modelInstance())->query(); |
||
| 83 | |||
| 84 | $this->filters()->apply($builder); |
||
| 85 | |||
| 86 | $builder = $this->indexBuilder($builder); |
||
| 87 | |||
| 88 | $builder = $this->indexSorting($builder); |
||
| 89 | |||
| 90 | if ($this->paginated) { |
||
| 91 | return $this->indexPagination($builder); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $builder->get()->map(function ($model) { |
||
| 95 | return (new static($this->registration))->manage($model); |
||
| 96 | }); |
||
| 97 | } |
||
| 98 | |||
| 99 | protected function indexBuilder(Builder $builder): Builder |
||
| 100 | { |
||
| 101 | return $builder; |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function indexSorting(Builder $builder): Builder |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function indexPagination($builder): Paginator |
||
| 127 | } |
||
| 128 | |||
| 129 | public function indexView(): string |
||
| 130 | { |
||
| 131 | return 'chief::back.managers._partials._rowitems'; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function indexViewData(): array |
||
| 135 | { |
||
| 136 | return []; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function modelInstance(): ManagedModel |
||
| 140 | { |
||
| 141 | $class = $this->registration->model(); |
||
| 142 | |||
| 143 | return new $class(); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function existingModel(): ManagedModel |
||
| 147 | { |
||
| 148 | if (!$this->hasExistingModel()) { |
||
| 149 | throw new NonExistingRecord('Model does not exist yet but is expected.'); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->model; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function hasExistingModel(): bool |
||
| 156 | { |
||
| 157 | return ($this->model && $this->model->exists); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Determine which actions should be available for this |
||
| 162 | * manager and their respective routed urls. |
||
| 163 | * |
||
| 164 | * @param $verb |
||
| 165 | * @return null|string |
||
| 166 | * @throws NonExistingRecord |
||
| 167 | */ |
||
| 168 | public function route($verb): ?string |
||
| 169 | { |
||
| 170 | $routes = [ |
||
| 171 | 'index' => route('chief.back.managers.index', [$this->registration->key()]), |
||
| 172 | 'create' => route('chief.back.managers.create', [$this->registration->key()]), |
||
| 173 | 'store' => route('chief.back.managers.store', [$this->registration->key()]), |
||
| 174 | ]; |
||
| 175 | |||
| 176 | if (array_key_exists($verb, $routes)) { |
||
| 177 | return $routes[$verb] ?? null; |
||
| 178 | } |
||
| 179 | |||
| 180 | //These routes expect the model to be persisted in the database |
||
| 181 | $modelRoutes = [ |
||
| 182 | 'edit' => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]), |
||
| 183 | 'update' => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]), |
||
| 184 | 'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]), |
||
| 185 | 'upload' => route('chief.back.managers.media.upload', [ |
||
| 186 | $this->registration->key(), |
||
| 187 | $this->existingModel()->id, |
||
| 188 | ]), |
||
| 189 | ]; |
||
| 190 | |||
| 191 | return $modelRoutes[$verb] ?? null; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function can($verb): bool |
||
| 195 | { |
||
| 196 | foreach (static::$bootedTraitMethods['can'] as $method) { |
||
| 197 | if (!method_exists($this, $method)) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | $this->$method($verb); |
||
| 201 | } |
||
| 202 | |||
| 203 | return !is_null($this->route($verb)); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function guard($verb): Manager |
||
| 207 | { |
||
| 208 | if (!$this->can($verb)) { |
||
| 209 | NotAllowedManagerRoute::notAllowedVerb($verb, $this); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function fields(): Fields |
||
| 216 | { |
||
| 217 | return new Fields(); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Enrich the manager fields with any of the assistant specified fields |
||
| 222 | * |
||
| 223 | * @return Fields |
||
| 224 | * @throws \Exception |
||
| 225 | */ |
||
| 226 | public function fieldsWithAssistantFields(): Fields |
||
| 239 | } |
||
| 240 | |||
| 241 | public function createFields(): Fields |
||
| 242 | { |
||
| 243 | return $this->fieldsWithAssistantFields(); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function editFields(): Fields |
||
| 247 | { |
||
| 248 | return $this->fieldsWithAssistantFields()->map(function (Field $field) { |
||
| 249 | return $field->model($this->model); |
||
| 250 | }); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function createView(): string |
||
| 254 | { |
||
| 255 | return 'chief::back.managers._partials._form'; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function createViewData(): array |
||
| 259 | { |
||
| 260 | return []; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function editView(): string |
||
| 264 | { |
||
| 265 | return 'chief::back.managers._partials._form'; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function editViewData(): array |
||
| 269 | { |
||
| 270 | return []; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function delete() |
||
| 274 | { |
||
| 275 | $this->guard('delete'); |
||
| 276 | |||
| 277 | if ($this->model instanceof ActsAsChild) { |
||
| 278 | $this->model->detachAllParentRelations(); |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($this->model instanceof ActsAsParent) { |
||
| 282 | $this->model->detachAllChildRelations(); |
||
| 283 | } |
||
| 284 | |||
| 285 | $this->model->delete(); |
||
| 286 | } |
||
| 287 | |||
| 288 | public static function filters(): Filters |
||
| 289 | { |
||
| 290 | return new Filters(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * This method can be used to manipulate the store request payload |
||
| 295 | * before being passed to the storing / updating the models. |
||
| 296 | * |
||
| 297 | * @param Request $request |
||
| 298 | * @return Request |
||
| 299 | */ |
||
| 300 | public function storeRequest(Request $request): Request |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * This method can be used to manipulate the update request payload |
||
| 307 | * before being passed to the storing / updating the models. |
||
| 308 | * |
||
| 309 | * @param Request $request |
||
| 310 | * @return Request |
||
| 311 | */ |
||
| 312 | public function updateRequest(Request $request): Request |
||
| 313 | { |
||
| 314 | return $request; |
||
| 315 | } |
||
| 316 | |||
| 317 | protected function requestContainsTranslations(Request $request): bool |
||
| 318 | { |
||
| 319 | return $request->has('trans'); |
||
| 320 | } |
||
| 321 | |||
| 322 | protected function validateConstraints() |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | public static function bootTraitMethods() |
||
| 330 | { |
||
| 331 | $class = static::class; |
||
| 332 | |||
| 333 | $methods = [ |
||
| 334 | 'can', |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 |