| Total Complexity | 59 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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 |
||
| 27 | abstract class AbstractManager |
||
| 28 | { |
||
| 29 | use SavingFields; |
||
| 30 | use HasDetails; |
||
|
|
|||
| 31 | use HasDetailSections; |
||
| 32 | use ManagesMedia; |
||
| 33 | use ManagesPagebuilder; |
||
| 34 | use TranslatableCommand; |
||
| 35 | use ManagesAssistants; |
||
| 36 | use ManagesFragments; |
||
| 37 | |||
| 38 | protected $translation_columns = []; |
||
| 39 | |||
| 40 | protected $model; |
||
| 41 | |||
| 42 | /** @var Register */ |
||
| 43 | protected $registration; |
||
| 44 | |||
| 45 | protected $pageCount = 20; |
||
| 46 | protected $paginated = true; |
||
| 47 | protected static $bootedTraitMethods = []; |
||
| 48 | |||
| 49 | final public function __construct(Registration $registration) |
||
| 50 | { |
||
| 51 | $this->registration = $registration; |
||
| 52 | |||
| 53 | // Upon instantiation, a general model is set that doesn't point to a persisted record. |
||
| 54 | $this->manage(app($this->registration->model())); |
||
| 55 | |||
| 56 | // Check if key and model are present since the model should be set by the manager itself |
||
| 57 | $this->validateConstraints(); |
||
| 58 | |||
| 59 | static::bootTraitMethods(); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function managerKey(): string |
||
| 63 | { |
||
| 64 | return $this->registration->key(); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function manage($model): Manager |
||
| 68 | { |
||
| 69 | $this->model = $model; |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function isManualSortable(): bool |
||
| 75 | { |
||
| 76 | return (isset($this->useManualSorting) && $this->useManualSorting && method_exists($this->modelInstance(), 'scopeSortedManually')); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function saveCreateFields(Request $request): void |
||
| 80 | { |
||
| 81 | if($this->isManualSortable() && !$request->has('order')) { |
||
| 82 | $this->model->order = $this->modelInstance()::orderBy('order', 'desc')->first()->order + 1; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->saveFields($request, $this->createFields()); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function findManaged($id): Manager |
||
| 89 | { |
||
| 90 | $modelInstance = $this->modelInstance()::where('id', $id)->withoutGlobalScopes()->first(); |
||
| 91 | |||
| 92 | return (new static($this->registration))->manage($modelInstance); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function indexCollection() |
||
| 96 | { |
||
| 97 | $builder = ($this->modelInstance())->query(); |
||
| 98 | |||
| 99 | $this->filters()->apply($builder); |
||
| 100 | |||
| 101 | $builder = $this->indexBuilder($builder); |
||
| 102 | |||
| 103 | $builder = $this->indexSorting($builder); |
||
| 104 | |||
| 105 | if ($this->paginated) { |
||
| 106 | return $this->indexPagination($builder); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $builder->get()->map(function ($model) { |
||
| 110 | return (new static($this->registration))->manage($model); |
||
| 111 | }); |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function indexBuilder(Builder $builder): Builder |
||
| 115 | { |
||
| 116 | return $builder; |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function indexSorting(Builder $builder): Builder |
||
| 120 | { |
||
| 121 | if($this->isManualSortable()) { |
||
| 122 | $builder->sortedManually(); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($this->isAssistedBy('publish') && Schema::hasColumn($this->modelInstance()->getTable(), 'published_at')) { |
||
| 126 | $builder->orderBy('published_at', 'DESC'); |
||
| 127 | } |
||
| 128 | |||
| 129 | // if model has no timestamps, updated_at doesn't exist |
||
| 130 | if ($this->modelInstance()->timestamps) { |
||
| 131 | $builder->orderBy('updated_at', 'DESC'); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $builder; |
||
| 135 | } |
||
| 136 | |||
| 137 | protected function indexPagination($builder): Paginator |
||
| 138 | { |
||
| 139 | $paginator = $builder->paginate($this->pageCount); |
||
| 140 | |||
| 141 | $modifiedCollection = $builder->paginate($this->pageCount)->getCollection()->transform(function ($model) { |
||
| 142 | return (new static($this->registration))->manage($model); |
||
| 143 | }); |
||
| 144 | |||
| 145 | return $paginator->setCollection($modifiedCollection); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function indexView(): string |
||
| 149 | { |
||
| 150 | return 'chief::back.managers._partials._rowitems'; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function indexViewData(): array |
||
| 154 | { |
||
| 155 | return []; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function modelInstance(): ManagedModel |
||
| 159 | { |
||
| 160 | $class = $this->registration->model(); |
||
| 161 | |||
| 162 | return new $class(); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function existingModel(): ManagedModel |
||
| 166 | { |
||
| 167 | if (!$this->hasExistingModel()) { |
||
| 168 | throw new NonExistingRecord('Model does not exist yet but is expected.'); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $this->model; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function hasExistingModel(): bool |
||
| 175 | { |
||
| 176 | return ($this->model && $this->model->exists); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Determine which actions should be available for this |
||
| 181 | * manager and their respective routed urls. |
||
| 182 | * |
||
| 183 | * @param $verb |
||
| 184 | * @return null|string |
||
| 185 | * @throws NonExistingRecord |
||
| 186 | */ |
||
| 187 | public function route($verb): ?string |
||
| 188 | { |
||
| 189 | $routes = [ |
||
| 190 | 'index' => route('chief.back.managers.index', [$this->registration->key()]), |
||
| 191 | 'create' => route('chief.back.managers.create', [$this->registration->key()]), |
||
| 192 | 'store' => route('chief.back.managers.store', [$this->registration->key()]), |
||
| 193 | ]; |
||
| 194 | |||
| 195 | if (array_key_exists($verb, $routes)) { |
||
| 196 | return $routes[$verb] ?? null; |
||
| 197 | } |
||
| 198 | |||
| 199 | //These routes expect the model to be persisted in the database |
||
| 200 | $modelRoutes = [ |
||
| 201 | 'edit' => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]), |
||
| 202 | 'update' => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]), |
||
| 203 | 'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]), |
||
| 204 | 'upload' => route('chief.back.managers.media.upload', [ |
||
| 205 | $this->registration->key(), |
||
| 206 | $this->existingModel()->id, |
||
| 207 | ]), |
||
| 208 | ]; |
||
| 209 | |||
| 210 | return $modelRoutes[$verb] ?? null; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function can($verb): bool |
||
| 214 | { |
||
| 215 | foreach (static::$bootedTraitMethods['can'] as $method) { |
||
| 216 | if (!method_exists($this, $method)) { |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | $this->$method($verb); |
||
| 220 | } |
||
| 221 | |||
| 222 | return !is_null($this->route($verb)); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function guard($verb): Manager |
||
| 226 | { |
||
| 227 | if (!$this->can($verb)) { |
||
| 228 | NotAllowedManagerRoute::notAllowedVerb($verb, $this); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function fields(): Fields |
||
| 235 | { |
||
| 236 | return new Fields(); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Enrich the manager fields with any of the assistant specified fields |
||
| 241 | * |
||
| 242 | * @return Fields |
||
| 243 | * @throws \Exception |
||
| 244 | */ |
||
| 245 | public function fieldsWithAssistantFields(): Fields |
||
| 258 | } |
||
| 259 | |||
| 260 | public function createFields(): Fields |
||
| 261 | { |
||
| 262 | $fields = $this->fieldsWithAssistantFields(); |
||
| 263 | |||
| 264 | if($this->isManualSortable() && !$fields->offsetExists('order')) { |
||
| 265 | $fields = $fields->add(NumberField::make('order')); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $fields; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function editFields(): Fields |
||
| 272 | { |
||
| 273 | return $this->fieldsWithAssistantFields()->map(function (Field $field) { |
||
| 274 | return $field->model($this->model); |
||
| 275 | }); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function createView(): string |
||
| 279 | { |
||
| 280 | return 'chief::back.managers._partials._form'; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function createViewData(): array |
||
| 284 | { |
||
| 285 | return []; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function editView(): string |
||
| 289 | { |
||
| 290 | return 'chief::back.managers._partials._form'; |
||
| 291 | } |
||
| 292 | |||
| 293 | public function editViewData(): array |
||
| 294 | { |
||
| 295 | return []; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function delete() |
||
| 299 | { |
||
| 300 | $this->guard('delete'); |
||
| 301 | |||
| 302 | if ($this->model instanceof ActsAsChild) { |
||
| 303 | $this->model->detachAllParentRelations(); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($this->model instanceof ActsAsParent) { |
||
| 307 | $this->model->detachAllChildRelations(); |
||
| 308 | } |
||
| 309 | |||
| 310 | $this->model->delete(); |
||
| 311 | } |
||
| 312 | |||
| 313 | public static function filters(): Filters |
||
| 314 | { |
||
| 315 | return new Filters(); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * This method can be used to manipulate the store request payload |
||
| 320 | * before being passed to the storing / updating the models. |
||
| 321 | * |
||
| 322 | * @param Request $request |
||
| 323 | * @return Request |
||
| 324 | */ |
||
| 325 | public function storeRequest(Request $request): Request |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * This method can be used to manipulate the update request payload |
||
| 332 | * before being passed to the storing / updating the models. |
||
| 333 | * |
||
| 334 | * @param Request $request |
||
| 335 | * @return Request |
||
| 336 | */ |
||
| 337 | public function updateRequest(Request $request): Request |
||
| 338 | { |
||
| 339 | return $request; |
||
| 340 | } |
||
| 341 | |||
| 342 | protected function requestContainsTranslations(Request $request): bool |
||
| 343 | { |
||
| 344 | return $request->has('trans'); |
||
| 345 | } |
||
| 346 | |||
| 347 | protected function validateConstraints() |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | public static function bootTraitMethods() |
||
| 355 | { |
||
| 356 | $class = static::class; |
||
| 357 | |||
| 358 | $methods = [ |
||
| 359 | 'can', |
||
| 360 | ]; |
||
| 361 | |||
| 362 | foreach ($methods as $baseMethod) { |
||
| 363 | static::$bootedTraitMethods[$baseMethod] = []; |
||
| 364 | |||
| 365 | foreach (class_uses_recursive($class) as $trait) { |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 |