| Total Complexity | 50 |
| Total Lines | 317 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Capsule 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 Capsule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Capsule |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public $name; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $path; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | public $enabled; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | public $packageCapsule = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | public $loaded = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string|null |
||
| 42 | */ |
||
| 43 | private $singular; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $namespace; |
||
| 49 | |||
| 50 | public function __construct( |
||
| 66 | } |
||
| 67 | |||
| 68 | public function boot(): void |
||
| 69 | { |
||
| 70 | $this->autoloadConfigFiles(); |
||
| 71 | $this->registerServiceProvider(); |
||
| 72 | $this->registerViews(); |
||
| 73 | $this->loadMigrations(); |
||
| 74 | |||
| 75 | if ($this->packageCapsule) { |
||
| 76 | $this->registerConfig(); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->registerRoutes(); |
||
| 80 | |||
| 81 | $this->loaded = true; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function registerServiceProvider(): void |
||
| 85 | { |
||
| 86 | $serviceProviderName = $this->name . 'CapsuleServiceProvider'; |
||
| 87 | |||
| 88 | if (File::exists($this->path . '/' . $serviceProviderName . '.php')) { |
||
| 89 | // @todo: test. |
||
| 90 | App::register($this->namespace . '\\' . $serviceProviderName); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | public function autoloadConfigFiles(): void |
||
| 95 | { |
||
| 96 | $files = $this->getConfig()['autoload']['files'] ?? null; |
||
| 97 | |||
| 98 | if (blank($files)) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | collect($files)->each(function ($file) { |
||
| 103 | if (file_exists($file)) { |
||
| 104 | require_once $file; |
||
| 105 | } |
||
| 106 | }); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function registerViews(): void |
||
| 110 | { |
||
| 111 | View::addLocation(Str::replaceLast('/' . $this->name, '', $this->path)); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function loadMigrations(): void |
||
| 115 | { |
||
| 116 | $callback = function (Migrator $migrator) { |
||
| 117 | $migrator->path($this->getMigrationsPath()); |
||
| 118 | }; |
||
| 119 | |||
| 120 | App()->afterResolving('migrator', $callback); |
||
| 121 | |||
| 122 | if (app()->resolved('migrator')) { |
||
| 123 | $callback(App::make('migrator')); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | public function registerRoutes(): void { |
||
| 128 | TwillRoutes::registerCapsuleRoutes(App::get('router'), $this); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function getBasePath(string $path): string |
||
| 132 | { |
||
| 133 | $exploded = explode('/', $path); |
||
| 134 | return implode('/', array_pop($exploded)); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getModule(): string |
||
| 138 | { |
||
| 139 | return Str::camel($this->name); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getDisplayName(): string |
||
| 143 | { |
||
| 144 | return Str::studly($this->name); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getPlural(): string |
||
| 148 | { |
||
| 149 | return $this->name; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function getSingular(): string |
||
| 153 | { |
||
| 154 | return $this->singular ?? Str::singular($this->name); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getBaseNamespace(): string |
||
| 158 | { |
||
| 159 | $explodedNamespace = explode('\\', $this->namespace); |
||
| 160 | return implode('\\', array_pop($explodedNamespace)); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getDatabaseNamespace(): string |
||
| 164 | { |
||
| 165 | return $this->namespace . '\\Database'; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getDatabasePsr4Path(): string |
||
| 169 | { |
||
| 170 | return $this->path . '/database'; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getSeedsNamespace(): string |
||
| 174 | { |
||
| 175 | return $this->namespace . '\\Seeds\\Database'; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getSeedsPsr4Path(): string |
||
| 179 | { |
||
| 180 | return $this->getDatabasePsr4Path() . '/seeds'; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getMigrationsPath(): string |
||
| 184 | { |
||
| 185 | return $this->getDatabasePsr4Path() . '/migrations'; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getResourcesPath(): string |
||
| 189 | { |
||
| 190 | return $this->getPsr4Path() . '/resources'; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getLanguagesPath(): string |
||
| 194 | { |
||
| 195 | return $this->getResourcesPath() . '/lang'; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getViewsPath(): string |
||
| 199 | { |
||
| 200 | return $this->getResourcesPath() . '/views'; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getModelNamespace(): string |
||
| 204 | { |
||
| 205 | // @todo: config('twill.capsules.namespaces.models'); |
||
| 206 | return $this->namespace . '\\Models'; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function getModelsDir(): string |
||
| 210 | { |
||
| 211 | return $this->getPsr4Path() . '/Models'; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getRepositoriesNamespace(): string |
||
| 215 | { |
||
| 216 | // @todo: config('twill.capsules.namespaces.repositories'); |
||
| 217 | return $this->namespace . '\\Repositories'; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function getRepositoriesDir(): string |
||
| 221 | { |
||
| 222 | return $this->getPsr4Path() . '/Repositories'; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function getControllersNamespace(): string |
||
| 226 | { |
||
| 227 | // @todo: config('twill.capsules.namespaces.controllers'); |
||
| 228 | return $this->namespace . '\\Http\\Controllers'; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function getControllersDir(): string |
||
| 232 | { |
||
| 233 | return $this->getPsr4Path() . '/Http/Controllers'; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getRequestsNamespace(): string |
||
| 237 | { |
||
| 238 | // @todo: config('twill.capsules.namespaces.requests'); |
||
| 239 | return $this->namespace . '\\Http\\Requests'; |
||
| 240 | } |
||
| 241 | |||
| 242 | public function getRequestsDir(): string |
||
| 243 | { |
||
| 244 | return $this->getPsr4Path() . '/Http/Requests'; |
||
| 245 | } |
||
| 246 | |||
| 247 | public function getPsr4Path(): string |
||
| 248 | { |
||
| 249 | // @todo: config('twill.capsules.namespaces.subdir'); |
||
| 250 | return $this->path; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getViewPrefix(): string |
||
| 254 | { |
||
| 255 | return "{$this->getModule()}.resources.views.admin"; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getRoutesFile(): string |
||
| 259 | { |
||
| 260 | return $this->getPsr4Path() . '/routes/admin.php'; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function routesFileExists(): bool |
||
| 264 | { |
||
| 265 | return file_exists($this->getRoutesFile()); |
||
| 266 | } |
||
| 267 | |||
| 268 | public function getModel(): string |
||
| 269 | { |
||
| 270 | return $this->getModelNamespace() . '\\' . $this->getSingular(); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function getTranslationModel(): string |
||
| 274 | { |
||
| 275 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Translation'; |
||
| 276 | } |
||
| 277 | |||
| 278 | public function getSlugModel(): string |
||
| 279 | { |
||
| 280 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Slug'; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getRevisionModel(): string |
||
| 284 | { |
||
| 285 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Revision'; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function getRepositoryClass(): string |
||
| 289 | { |
||
| 290 | return $this->getRepositoriesNamespace() . '\\' . $this->getSingular() . 'Repository'; |
||
| 291 | } |
||
| 292 | |||
| 293 | public function getControllerClass(): string |
||
| 294 | { |
||
| 295 | return $this->getControllersNamespace() . '\\' . $this->getSingular() . 'Controller'; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function getFormRequestClass(): string |
||
| 301 | } |
||
| 302 | |||
| 303 | public function getConfigFile(): string |
||
| 304 | { |
||
| 305 | return $this->path . '/config.php'; |
||
| 306 | } |
||
| 307 | |||
| 308 | public function getConfig(): array |
||
| 314 | } |
||
| 315 | |||
| 316 | public function registerConfig(): void |
||
| 317 | { |
||
| 318 | $config = Config::get('twill-navigation', []); |
||
| 319 | |||
| 320 | $config[$this->name] = [ |
||
| 321 | 'title' => $this->name, |
||
| 326 | } |
||
| 327 | |||
| 328 | public function getType(): string { |
||
| 329 | return ''; |
||
| 330 | } |
||
| 331 | } |
||
| 332 |