Total Complexity | 49 |
Total Lines | 310 |
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) |
||
75 | } |
||
76 | |||
77 | public function register() |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | public function boot() |
||
90 | { |
||
91 | if ($this->areMigrationsEnabled()) { |
||
92 | $this->registerMigrations(); |
||
93 | } |
||
94 | |||
95 | if ($this->areModelsEnabled()) { |
||
96 | $this->registerModels(); |
||
97 | $this->registerEnums(); |
||
98 | $this->registerRequestTypes(); |
||
99 | } |
||
100 | |||
101 | if ($this->areViewsEnabled()) { |
||
102 | $this->registerViews(); |
||
103 | } |
||
104 | |||
105 | if ($routes = $this->config('routes', true)) { |
||
106 | $this->registerRoutes($routes); |
||
107 | } |
||
108 | |||
109 | $this->publishes([ |
||
110 | $this->getBasePath() . '/' . $this->convention->migrationsFolder() => |
||
111 | database_path('migrations') |
||
112 | ], 'migrations'); |
||
113 | } |
||
114 | |||
115 | public function areMigrationsEnabled(): bool |
||
116 | { |
||
117 | return (bool) $this->config('migrations', true); |
||
118 | } |
||
119 | |||
120 | public function areModelsEnabled(): bool |
||
121 | { |
||
122 | return (bool) $this->config('models', true); |
||
123 | } |
||
124 | |||
125 | public function areViewsEnabled(): bool |
||
126 | { |
||
127 | return (bool) $this->config('views', true); |
||
128 | } |
||
129 | |||
130 | public function areRoutesEnabled(): bool |
||
131 | { |
||
132 | return (bool) $this->config('routes', true); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @inheritDoc |
||
137 | */ |
||
138 | public function getId(): string |
||
139 | { |
||
140 | return $this->id; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns module configuration value(s) |
||
145 | * |
||
146 | * @param string $key If left empty, the entire module configuration gets retrieved |
||
147 | * @param mixed $default |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function config(string $key = null, $default = null) |
||
152 | { |
||
153 | $key = $key ? sprintf('%s.%s', $this->getId(), $key) : $this->getId(); |
||
154 | |||
155 | return config($key, $default); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return Manifest |
||
160 | */ |
||
161 | public function getManifest(): Manifest |
||
162 | { |
||
163 | if (!$this->manifest) { |
||
164 | $data = include($this->basePath . '/' . $this->convention->manifestFile()); |
||
165 | |||
166 | $name = $data['name'] ?? 'N/A'; |
||
167 | $version = $data['version'] ?? 'n/a'; |
||
168 | |||
169 | $this->manifest = new Manifest($name, $version); |
||
170 | } |
||
171 | |||
172 | return $this->manifest; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns the root folder on the filesystem containing the module |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getBasePath(): string |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | public function getKind(): Kind |
||
189 | { |
||
190 | return $this->kind; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns the folder where the module/box configuration files are |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getConfigPath(): string |
||
199 | { |
||
200 | return $this->getBasePath() . '/' . $this->convention->configFolder(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Returns the module's root (topmost) namespace |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getNamespaceRoot(): string |
||
209 | { |
||
210 | return $this->namespaceRoot; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Returns the short (abbreviated) name of the module |
||
215 | * E.g. Konekt\AppShell => app_shell |
||
216 | */ |
||
217 | public function shortName() |
||
218 | { |
||
219 | $id = $this->getModuleId(); |
||
220 | $p = strrpos($id, '.'); |
||
221 | |||
222 | return $p ? substr($id, $p + 1) : $id; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns a standard module name based on the module provider's classname |
||
227 | * |
||
228 | * Eg.: '\Vendor\Module\Services\ModuleServiceProvider' -> 'vendor.module' |
||
229 | * |
||
230 | * @param string $classname |
||
231 | * |
||
232 | * @see concord_module_id |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getModuleId($classname = null) |
||
237 | { |
||
238 | return concord_module_id($classname ?: static::class); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Register the module's migrations |
||
243 | */ |
||
244 | protected function registerMigrations() |
||
245 | { |
||
246 | $path = $this->getBasePath() . '/' . $this->convention->migrationsFolder(); |
||
247 | |||
248 | if ($this->app->runningInConsole() && is_dir($path)) { |
||
249 | $this->loadMigrationsFrom($path); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Register models in a box/module |
||
255 | */ |
||
256 | protected function registerModels() |
||
257 | { |
||
258 | foreach ($this->models as $key => $model) { |
||
259 | $contract = is_string($key) ? $key : $this->convention->contractForModel($model); |
||
260 | $this->concord->registerModel($contract, $model); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Register enums in a box/module |
||
266 | */ |
||
267 | protected function registerEnums() |
||
268 | { |
||
269 | foreach ($this->enums as $key => $enum) { |
||
270 | $contract = is_string($key) ? $key : $this->convention->contractForEnum($enum); |
||
271 | $this->concord->registerEnum($contract, $enum); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Register request types in a box/module |
||
277 | */ |
||
278 | protected function registerRequestTypes() |
||
279 | { |
||
280 | foreach ($this->requests as $key => $requestType) { |
||
281 | $contract = is_string($key) ? $key : $this->convention->contractForRequest($requestType); |
||
282 | $this->concord->registerRequest($contract, $requestType); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Register the views folder, in a separate namespace |
||
288 | */ |
||
289 | protected function registerViews() |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Registers the event service provider of the module/config (ie. event-listener bindings) |
||
301 | */ |
||
302 | protected function registerEventServiceProvider() |
||
303 | { |
||
304 | $eventServiceProviderClass = sprintf('%s\\%s\\EventServiceProvider', |
||
305 | $this->namespaceRoot, |
||
306 | str_replace('/', '\\', $this->convention->providersFolder()) |
||
307 | ); |
||
308 | |||
309 | if (class_exists($eventServiceProviderClass)) { |
||
310 | $this->app->register($eventServiceProviderClass); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | protected function loadConfiguration() |
||
320 | } |
||
321 | } |
||
322 | |||
323 | protected function registerRoutes($routes): void |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 |