| Total Complexity | 68 |
| Total Lines | 652 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 2 | Features | 0 |
Complex classes like ModelConfigurationManager 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 ModelConfigurationManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class ModelConfigurationManager implements ModelConfigurationInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Get the event dispatcher instance. |
||
| 30 | * |
||
| 31 | * @return \Illuminate\Contracts\Events\Dispatcher |
||
| 32 | */ |
||
| 33 | public static function getEventDispatcher() |
||
| 34 | { |
||
| 35 | return self::$dispatcher; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Set the event dispatcher instance. |
||
| 40 | * |
||
| 41 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public static function setEventDispatcher(Dispatcher $dispatcher) |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The event dispatcher instance. |
||
| 51 | * |
||
| 52 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
| 53 | */ |
||
| 54 | protected static $dispatcher; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 58 | */ |
||
| 59 | protected $app; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $class; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Model |
||
| 68 | */ |
||
| 69 | protected $model; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $alias; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string|null |
||
| 78 | */ |
||
| 79 | protected $controllerClass; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $title; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $icon; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected $checkAccess = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $redirect = ['edit' => 'edit', 'create' => 'edit']; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var RepositoryInterface |
||
| 103 | */ |
||
| 104 | private $repository; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var Model|null |
||
| 108 | */ |
||
| 109 | protected $model_value = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * ModelConfigurationManager constructor. |
||
| 113 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 114 | * @param $class |
||
| 115 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
| 116 | * @throws \SleepingOwl\Admin\Exceptions\RepositoryException |
||
| 117 | */ |
||
| 118 | public function __construct(\Illuminate\Contracts\Foundation\Application $app, $class) |
||
| 119 | { |
||
| 120 | $this->app = $app; |
||
| 121 | $this->class = $class; |
||
| 122 | |||
| 123 | $this->model = $app->make($class); |
||
| 124 | |||
| 125 | $this->repository = $app->make(RepositoryInterface::class); |
||
| 126 | $this->repository->setClass($class); |
||
| 127 | if (! $this->alias) { |
||
| 128 | $this->setDefaultAlias(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getClass() |
||
| 136 | { |
||
| 137 | return $this->class; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return Model |
||
| 142 | */ |
||
| 143 | public function getModel() |
||
| 144 | { |
||
| 145 | return $this->model; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return Model|null |
||
| 150 | */ |
||
| 151 | public function getModelValue() |
||
| 152 | { |
||
| 153 | return $this->model_value; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param Model $item |
||
| 158 | */ |
||
| 159 | public function setModelValue($item) |
||
| 160 | { |
||
| 161 | $this->model_value = $item; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getTitle() |
||
| 168 | { |
||
| 169 | if (is_null($this->title)) { |
||
|
|
|||
| 170 | $title = str_replace('_', ' ', $this->getDefaultClassTitle()); |
||
| 171 | $this->title = ucwords($title); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->title; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getIcon() |
||
| 181 | { |
||
| 182 | return $this->icon; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $icon |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setIcon($icon) |
||
| 191 | { |
||
| 192 | $this->icon = $icon; |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getAlias() |
||
| 201 | { |
||
| 202 | return $this->alias; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return RepositoryInterface |
||
| 207 | */ |
||
| 208 | public function getRepository() |
||
| 209 | { |
||
| 210 | return $this->repository; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getCreateTitle() |
||
| 217 | { |
||
| 218 | return trans('sleeping_owl::lang.model.create', ['title' => $this->getTitle()]); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getEditTitle() |
||
| 225 | { |
||
| 226 | return trans('sleeping_owl::lang.model.edit', ['title' => $this->getTitle()]); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function isDisplayable() |
||
| 233 | { |
||
| 234 | return $this->can('display', $this->getModel()); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function isCreatable() |
||
| 241 | { |
||
| 242 | return $this->can('create', $this->getModel()); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param Model $model |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isEditable(Model $model) |
||
| 251 | { |
||
| 252 | return $this->can('edit', $model); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param Model $model |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function isDeletable(Model $model) |
||
| 261 | { |
||
| 262 | return $this->can('delete', $model); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param Model $model |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isDestroyable(Model $model) |
||
| 271 | { |
||
| 272 | return $this->isRestorableModel() && $this->can('destroy', $model); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param Model $model |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function isRestorable(Model $model) |
||
| 281 | { |
||
| 282 | return $this->isRestorableModel() && $this->can('restore', $model); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function isRestorableModel() |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param int $id |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | * @deprecated |
||
| 298 | */ |
||
| 299 | public function fireFullEdit($id) |
||
| 300 | { |
||
| 301 | return $this->fireEdit($id); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function enableAccessCheck() |
||
| 308 | { |
||
| 309 | $this->checkAccess = true; |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function disableAccessCheck() |
||
| 318 | { |
||
| 319 | $this->checkAccess = false; |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $action |
||
| 326 | * @param Model $model |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function can($action, Model $model) |
||
| 331 | { |
||
| 332 | if (! $this->checkAccess) { |
||
| 333 | return true; |
||
| 334 | } |
||
| 335 | |||
| 336 | return \Gate::allows($action, [$this, $model]); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $controllerClass |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function setControllerClass($controllerClass) |
||
| 345 | { |
||
| 346 | $this->controllerClass = $controllerClass; |
||
| 347 | |||
| 348 | return $this; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return null|string |
||
| 353 | */ |
||
| 354 | public function getControllerClass() |
||
| 355 | { |
||
| 356 | return $this->controllerClass; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return null|string|bool |
||
| 361 | */ |
||
| 362 | public function hasCustomControllerClass() |
||
| 363 | { |
||
| 364 | $controller = $this->getControllerClass(); |
||
| 365 | |||
| 366 | return ! is_null($controller) && class_exists($controller); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param array $parameters |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function getDisplayUrl(array $parameters = []) |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $parameters |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function getCancelUrl(array $parameters = []) |
||
| 387 | { |
||
| 388 | return $this->getDisplayUrl($parameters); |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $parameters |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function getCreateUrl(array $parameters = []) |
||
| 397 | { |
||
| 398 | array_unshift($parameters, $this->getAlias()); |
||
| 399 | |||
| 400 | return route('admin.model.create', $parameters); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param array $parameters |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getStoreUrl(array $parameters = []) |
||
| 409 | { |
||
| 410 | array_unshift($parameters, $this->getAlias()); |
||
| 411 | |||
| 412 | return route('admin.model.store', $parameters); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string|int $id |
||
| 417 | * @param array $parameters |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getEditUrl($id, array $parameters = []) |
||
| 422 | { |
||
| 423 | array_unshift($parameters, $this->getAlias(), $id); |
||
| 424 | |||
| 425 | return route('admin.model.edit', $parameters); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string|int $id |
||
| 430 | * @param array $parameters |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function getUpdateUrl($id, array $parameters = []) |
||
| 435 | { |
||
| 436 | array_unshift($parameters, $this->getAlias(), $id); |
||
| 437 | |||
| 438 | return route('admin.model.update', $parameters); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param string|int $id |
||
| 443 | * @param array $parameters |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getDeleteUrl($id, array $parameters = []) |
||
| 448 | { |
||
| 449 | array_unshift($parameters, $this->getAlias(), $id); |
||
| 450 | |||
| 451 | return route('admin.model.delete', $parameters); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string|int $id |
||
| 456 | * @param array $parameters |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getDestroyUrl($id, array $parameters = []) |
||
| 461 | { |
||
| 462 | array_unshift($parameters, $this->getAlias(), $id); |
||
| 463 | |||
| 464 | return route('admin.model.destroy', $parameters); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string|int $id |
||
| 469 | * @param array $parameters |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getRestoreUrl($id, array $parameters = []) |
||
| 474 | { |
||
| 475 | array_unshift($parameters, $this->getAlias(), $id); |
||
| 476 | |||
| 477 | return route('admin.model.restore', $parameters); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getMessageOnCreate() |
||
| 484 | { |
||
| 485 | return trans('sleeping_owl::lang.message.created'); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | public function getMessageOnUpdate() |
||
| 492 | { |
||
| 493 | return trans('sleeping_owl::lang.message.updated'); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function getMessageOnDelete() |
||
| 500 | { |
||
| 501 | return trans('sleeping_owl::lang.message.deleted'); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getMessageOnRestore() |
||
| 508 | { |
||
| 509 | return trans('sleeping_owl::lang.message.restored'); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public function getMessageOnDestroy() |
||
| 516 | { |
||
| 517 | return trans('sleeping_owl::lang.message.destroyed'); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return Navigation |
||
| 522 | */ |
||
| 523 | public function getNavigation() |
||
| 524 | { |
||
| 525 | return $this->app['sleeping_owl.navigation']; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param int $priority |
||
| 530 | * @param string|\Closure|BadgeInterface $badge |
||
| 531 | * |
||
| 532 | * @return Page |
||
| 533 | */ |
||
| 534 | public function addToNavigation($priority = 100, $badge = null) |
||
| 535 | { |
||
| 536 | $page = $this->makePage($priority, $badge); |
||
| 537 | |||
| 538 | $this->getNavigation()->addPage($page); |
||
| 539 | |||
| 540 | return $page; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param $page_id |
||
| 545 | * @return \KodiComponents\Navigation\Contracts\PageInterface|null |
||
| 546 | */ |
||
| 547 | public function getNavigationPage($page_id) |
||
| 548 | { |
||
| 549 | return $this->getNavigation()->getPages()->findById($page_id); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param int $priority |
||
| 554 | * @param string|\Closure|BadgeInterface $badge |
||
| 555 | * |
||
| 556 | * @return Page |
||
| 557 | */ |
||
| 558 | protected function makePage($priority = 100, $badge = null) |
||
| 559 | { |
||
| 560 | $page = new Page($this->getClass()); |
||
| 561 | $page->setPriority($priority); |
||
| 562 | |||
| 563 | if ($badge) { |
||
| 564 | if (! ($badge instanceof BadgeInterface)) { |
||
| 565 | $badge = new Badge($badge); |
||
| 566 | } |
||
| 567 | |||
| 568 | $page->setBadge($badge); |
||
| 569 | } |
||
| 570 | |||
| 571 | return $page; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param array $redirect |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | public function setRedirect(array $redirect) |
||
| 579 | { |
||
| 580 | $this->redirect = $redirect; |
||
| 581 | |||
| 582 | return $this; |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @return \Illuminate\Support\Collection |
||
| 587 | */ |
||
| 588 | public function getRedirect() |
||
| 589 | { |
||
| 590 | return collect($this->redirect); |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Fire the given event for the model. |
||
| 595 | * |
||
| 596 | * @param string $event |
||
| 597 | * @param bool $halt |
||
| 598 | * @param Model|null $model |
||
| 599 | * @param array $payload |
||
| 600 | * |
||
| 601 | * @return mixed |
||
| 602 | */ |
||
| 603 | public function fireEvent($event, $halt = true, Model $model = null, ...$payload) |
||
| 604 | { |
||
| 605 | if (! isset(self::$dispatcher)) { |
||
| 606 | return true; |
||
| 607 | } |
||
| 608 | |||
| 609 | if (is_null($model)) { |
||
| 610 | $model = $this->getModel(); |
||
| 611 | } |
||
| 612 | |||
| 613 | // We will append the names of the class to the event to distinguish it from |
||
| 614 | // other model events that are fired, allowing us to listen on each model |
||
| 615 | // event set individually instead of catching event for all the models. |
||
| 616 | $event = "sleeping_owl.section.{$event}: ".$this->getClass(); |
||
| 617 | |||
| 618 | // Laravel 5.8 and 5.4 support fire method |
||
| 619 | if (version_compare('5.8.0', $this->app->version(), '<=') || |
||
| 620 | version_compare('5.5.0', $this->app->version(), '>')) { |
||
| 621 | $fireMethod = 'dispatch'; |
||
| 622 | } else { |
||
| 623 | $fireMethod = 'fire'; |
||
| 624 | } |
||
| 625 | |||
| 626 | $method = $halt ? 'until' : $fireMethod; |
||
| 627 | |||
| 628 | array_unshift($payload, $this, $model); |
||
| 629 | |||
| 630 | return self::$dispatcher->$method($event, $payload); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Handle dynamic method calls into the model. |
||
| 635 | * |
||
| 636 | * @param $method |
||
| 637 | * @param $arguments |
||
| 638 | * |
||
| 639 | * @return mixed |
||
| 640 | */ |
||
| 641 | public function __call($method, $arguments) |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param $event |
||
| 657 | * @param $callback |
||
| 658 | * @param int $priority |
||
| 659 | */ |
||
| 660 | protected function registerEvent($event, $callback, $priority = 0) |
||
| 661 | { |
||
| 662 | if (isset(self::$dispatcher)) { |
||
| 663 | self::$dispatcher->listen("sleeping_owl.section.{$event}: ".$this->getClass(), $callback, $priority); |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | protected function setDefaultAlias() |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | protected function getDefaultClassTitle() |
||
| 676 | { |
||
| 677 | return Str::snake(Str::plural(class_basename($this->getClass()))); |
||
| 678 | } |
||
| 679 | } |
||
| 680 |