Complex classes like ServiceProvider 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
30 | |||
31 | /** |
||
32 | * Holds the {@see AbstractData} instances. |
||
33 | */ |
||
34 | protected $datas; |
||
35 | |||
36 | /** |
||
37 | * Initializes needed but yet missing service providers. |
||
38 | * |
||
39 | * @param Container $app |
||
40 | * the application container |
||
41 | */ |
||
42 | 78 | protected function initMissingServiceProviders(Container $app) { |
|
60 | |||
61 | /** |
||
62 | * Initializes the available locales. |
||
63 | * |
||
64 | * @param Container $app |
||
65 | * the application container |
||
66 | * |
||
67 | * @return array |
||
68 | * the available locales |
||
69 | */ |
||
70 | 78 | protected function initLocales(Container $app) { |
|
79 | |||
80 | /** |
||
81 | * Initializes the children of the data entries. |
||
82 | */ |
||
83 | 78 | protected function initChildren() { |
|
93 | |||
94 | /** |
||
95 | * Gets a map with localized entity labels from the CRUD YML. |
||
96 | * |
||
97 | * @param array $locales |
||
98 | * the available locales |
||
99 | * @param array $crud |
||
100 | * the CRUD entity map |
||
101 | * |
||
102 | * @return array |
||
103 | * the map with localized entity labels |
||
104 | */ |
||
105 | 78 | protected function getLocaleLabels($locales, $crud) { |
|
114 | |||
115 | /** |
||
116 | * Configures the EntityDefinition according to the given |
||
117 | * CRUD entity map. |
||
118 | * |
||
119 | * @param EntityDefinition $definition |
||
120 | * the definition to configure |
||
121 | * @param array $crud |
||
122 | * the CRUD entity map |
||
123 | */ |
||
124 | 78 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
|
143 | |||
144 | /** |
||
145 | * Creates and setups an EntityDefinition instance. |
||
146 | * |
||
147 | * @param Container $app |
||
148 | * the application container |
||
149 | * @param array $locales |
||
150 | * the available locales |
||
151 | * @param array $crud |
||
152 | * the parsed YAML of a CRUD entity |
||
153 | * @param string $name |
||
154 | * the name of the entity |
||
155 | * |
||
156 | * @return EntityDefinition |
||
157 | * the EntityDefinition good to go |
||
158 | */ |
||
159 | 78 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
|
181 | |||
182 | /** |
||
183 | * Validates the parsed entity definition. |
||
184 | * |
||
185 | * @param Container $app |
||
186 | * the application container |
||
187 | * @param array $entityDefinition |
||
188 | * the entity definition to validate |
||
189 | */ |
||
190 | 78 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
|
199 | |||
200 | /** |
||
201 | * Initializes the instance. |
||
202 | * |
||
203 | * @param DataFactoryInterface $dataFactory |
||
204 | * the factory to create the concrete AbstractData instances |
||
205 | * @param string $crudFile |
||
206 | * the CRUD YAML file to parse |
||
207 | * @param string|null $crudFileCachingDirectory |
||
208 | * the writable directory to store the CRUD YAML file cache |
||
209 | * @param FileProcessorInterface $fileProcessor |
||
210 | * the file processor used for file fields |
||
211 | * @param Container $app |
||
212 | * the application container |
||
213 | */ |
||
214 | 79 | public function init(DataFactoryInterface $dataFactory, $crudFile, $crudFileCachingDirectory, FileProcessorInterface $fileProcessor, Container $app) { |
|
231 | |||
232 | /** |
||
233 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
234 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
235 | * |
||
236 | * @param Container $app |
||
237 | * the Container instance of the Silex application |
||
238 | */ |
||
239 | public function register(Container $app) { |
||
248 | |||
249 | /** |
||
250 | * Initializes the crud service right after boot. |
||
251 | * |
||
252 | * @param Application $app |
||
253 | * the Container instance of the Silex application |
||
254 | */ |
||
255 | 78 | public function boot(Application $app) { |
|
260 | |||
261 | /** |
||
262 | * Getter for the {@see AbstractData} instances. |
||
263 | * |
||
264 | * @param string $name |
||
265 | * the entity name of the desired Data instance |
||
266 | * |
||
267 | * @return AbstractData |
||
268 | * the AbstractData instance or null on invalid name |
||
269 | */ |
||
270 | 72 | public function getData($name) { |
|
276 | |||
277 | /** |
||
278 | * Getter for all available entity names. |
||
279 | * |
||
280 | * @return string[] |
||
281 | * a list of all available entity names |
||
282 | */ |
||
283 | 3 | public function getEntities() { |
|
286 | |||
287 | /** |
||
288 | * Getter for the entities for the navigation bar. |
||
289 | * |
||
290 | * @return string[] |
||
291 | * a list of all available entity names with their group |
||
292 | */ |
||
293 | 10 | public function getEntitiesNavBar() { |
|
305 | |||
306 | /** |
||
307 | * Determines the Twig template to use for the given parameters depending on |
||
308 | * the existance of certain keys in the Container $app in this order: |
||
309 | * |
||
310 | * crud.$section.$action.$entity |
||
311 | * crud.$section.$action |
||
312 | * crud.$section |
||
313 | * |
||
314 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
315 | * |
||
316 | * @param Container $app |
||
317 | * the Silex application |
||
318 | * @param string $section |
||
319 | * the section of the template, either "layout" or "template" |
||
320 | * @param string $action |
||
321 | * the current calling action like "create" or "show" |
||
322 | * @param string $entity |
||
323 | * the current calling entity |
||
324 | * |
||
325 | * @return string |
||
326 | * the best fitting template |
||
327 | */ |
||
328 | 11 | public function getTemplate(Container $app, $section, $action, $entity) { |
|
346 | |||
347 | /** |
||
348 | * Sets the locale to be used. |
||
349 | * |
||
350 | * @param string $locale |
||
351 | * the locale to be used. |
||
352 | */ |
||
353 | 10 | public function setLocale($locale) { |
|
358 | |||
359 | /** |
||
360 | * Gets the available locales. |
||
361 | * |
||
362 | * @return array |
||
363 | * the available locales |
||
364 | */ |
||
365 | 79 | public function getLocales() { |
|
382 | |||
383 | } |
||
384 |