| Total Complexity | 48 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 0 |
Complex classes like BaseServiceProvider 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 BaseServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class BaseServiceProvider extends ServiceProvider implements Module |
||
| 24 | { |
||
| 25 | /** @var string */ |
||
| 26 | protected $basePath; |
||
| 27 | |||
| 28 | /** @var Manifest */ |
||
| 29 | protected $manifest; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $namespaceRoot; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $id; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | protected $models = []; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | protected $enums = []; |
||
| 42 | |||
| 43 | /** @var array */ |
||
| 44 | protected $requests = []; |
||
| 45 | |||
| 46 | /** @var ConcordContract */ |
||
| 47 | protected $concord; |
||
| 48 | |||
| 49 | /** @var Convention */ |
||
| 50 | protected $convention; |
||
| 51 | |||
| 52 | /** @var Kind */ |
||
| 53 | protected $kind; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ModuleServiceProvider class constructor |
||
| 57 | * |
||
| 58 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 59 | */ |
||
| 60 | public function __construct($app) |
||
| 61 | { |
||
| 62 | parent::__construct($app); |
||
| 63 | |||
| 64 | $this->concord = $app->make(ConcordContract::class); // retrieve the concord singleton |
||
| 65 | $this->convention = $this->concord->getConvention(); // storing to get rid of train wrecks |
||
| 66 | $this->kind = Kind::create(static::$_kind); |
||
| 67 | $this->basePath = dirname(dirname((new ReflectionClass(static::class))->getFileName())); |
||
| 68 | $this->namespaceRoot = str_replace( |
||
| 69 | sprintf('\\%s\\ModuleServiceProvider', |
||
| 70 | str_replace('/', '\\', $this->convention->providersFolder()) |
||
| 71 | ), |
||
| 72 | '', static::class |
||
| 73 | ); |
||
| 74 | $this->id = $this->getModuleId(); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function register() |
||
| 78 | { |
||
| 79 | $this->loadConfiguration(); |
||
| 80 | |||
| 81 | if (true === $this->config('event_listeners')) { |
||
| 82 | $this->registerEventServiceProvider(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritdoc |
||
| 88 | */ |
||
| 89 | public function boot() |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public function areMigrationsEnabled(): bool |
||
| 120 | { |
||
| 121 | return (bool) $this->config('migrations', true); |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | public function areModelsEnabled(): bool |
||
| 126 | { |
||
| 127 | return (bool) $this->config('models', true); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function areViewsEnabled(): bool |
||
| 131 | { |
||
| 132 | return (bool) $this->config('views', true); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | public function areRoutesEnabled(): bool |
||
| 137 | { |
||
| 138 | return (bool) $this->config('routes', true); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritDoc |
||
| 143 | */ |
||
| 144 | public function getId(): string |
||
| 145 | { |
||
| 146 | return $this->id; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns module configuration value(s) |
||
| 151 | * |
||
| 152 | * @param string $key If left empty, the entire module configuration gets retrieved |
||
| 153 | * @param mixed $default |
||
| 154 | * |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | public function config(string $key = null, $default = null) |
||
| 158 | { |
||
| 159 | $key = $key ? sprintf('%s.%s', $this->getId(), $key) : $this->getId(); |
||
| 160 | |||
| 161 | return config($key, $default); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return Manifest |
||
| 166 | */ |
||
| 167 | public function getManifest(): Manifest |
||
| 168 | { |
||
| 169 | if (!$this->manifest) { |
||
| 170 | $data = include($this->basePath . '/' . $this->convention->manifestFile()); |
||
| 171 | |||
| 172 | $name = $data['name'] ?? 'N/A'; |
||
| 173 | $version = $data['version'] ?? 'n/a'; |
||
| 174 | |||
| 175 | $this->manifest = new Manifest($name, $version); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->manifest; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the root folder on the filesystem containing the module |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getBasePath(): string |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritdoc |
||
| 193 | */ |
||
| 194 | public function getKind(): Kind |
||
| 195 | { |
||
| 196 | return $this->kind; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the folder where the module/box configuration files are |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getConfigPath(): string |
||
| 205 | { |
||
| 206 | return $this->getBasePath() . '/' . $this->convention->configFolder(); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the module's root (topmost) namespace |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getNamespaceRoot(): string |
||
| 215 | { |
||
| 216 | return $this->namespaceRoot; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the short (abbreviated) name of the module |
||
| 221 | * E.g. Konekt\AppShell => app_shell |
||
| 222 | */ |
||
| 223 | public function shortName() |
||
| 224 | { |
||
| 225 | $id = $this->getModuleId(); |
||
| 226 | $p = strrpos($id, '.'); |
||
| 227 | |||
| 228 | return $p ? substr($id, $p + 1) : $id; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns a standard module name based on the module provider's classname |
||
| 233 | * |
||
| 234 | * Eg.: '\Vendor\Module\Services\ModuleServiceProvider' -> 'vendor.module' |
||
| 235 | * |
||
| 236 | * @param string $classname |
||
| 237 | * |
||
| 238 | * @see concord_module_id |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | protected function getModuleId($classname = null) |
||
| 243 | { |
||
| 244 | return concord_module_id($classname ?: static::class); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Register the module's migrations |
||
| 249 | */ |
||
| 250 | protected function registerMigrations() |
||
| 251 | { |
||
| 252 | $path = $this->getBasePath() . '/' . $this->convention->migrationsFolder(); |
||
| 253 | |||
| 254 | if ($this->app->runningInConsole() && is_dir($path)) { |
||
| 255 | $this->loadMigrationsFrom($path); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Register models in a box/module |
||
| 261 | */ |
||
| 262 | protected function registerModels() |
||
| 263 | { |
||
| 264 | foreach ($this->models as $key => $model) { |
||
| 265 | $contract = is_string($key) ? $key : $this->convention->contractForModel($model); |
||
| 266 | $this->concord->registerModel($contract, $model); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Register enums in a box/module |
||
| 272 | */ |
||
| 273 | protected function registerEnums() |
||
| 274 | { |
||
| 275 | foreach ($this->enums as $key => $enum) { |
||
| 276 | $contract = is_string($key) ? $key : $this->convention->contractForEnum($enum); |
||
| 277 | $this->concord->registerEnum($contract, $enum); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Register request types in a box/module |
||
| 283 | */ |
||
| 284 | protected function registerRequestTypes() |
||
| 285 | { |
||
| 286 | foreach ($this->requests as $key => $requestType) { |
||
| 287 | $contract = is_string($key) ? $key : $this->convention->contractForRequest($requestType); |
||
| 288 | $this->concord->registerRequest($contract, $requestType); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Register the views folder, in a separate namespace |
||
| 294 | */ |
||
| 295 | protected function registerViews() |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Registers the event service provider of the module/config (ie. event-listener bindings) |
||
| 307 | */ |
||
| 308 | protected function registerEventServiceProvider() |
||
| 309 | { |
||
| 310 | $eventServiceProviderClass = sprintf('%s\\%s\\EventServiceProvider', |
||
| 311 | $this->namespaceRoot, |
||
| 312 | str_replace('/', '\\', $this->convention->providersFolder()) |
||
| 313 | ); |
||
| 314 | |||
| 315 | if (class_exists($eventServiceProviderClass)) { |
||
| 316 | $this->app->register($eventServiceProviderClass); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | protected function loadConfiguration() |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } |
||
| 329 |