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 |
||
| 37 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holds the data instances. |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $datas; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Holds the map for overriding templates. |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $templates = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Holds whether CRUDlex manages i18n. |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $manageI18n = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Holds the URL generator. |
||
| 60 | * @var \Symfony\Component\Routing\Generator\UrlGenerator |
||
| 61 | */ |
||
| 62 | protected $urlGenerator; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Initializes needed but yet missing service providers. |
||
| 66 | * |
||
| 67 | * @param Container $app |
||
| 68 | * the application container |
||
| 69 | */ |
||
| 70 | 79 | protected function initMissingServiceProviders(Container $app) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Initializes the available locales. |
||
| 92 | * |
||
| 93 | * @param Translator $translator |
||
| 94 | * the translator |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | * the available locales |
||
| 98 | */ |
||
| 99 | 78 | protected function initLocales(Translator $translator) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Initializes the children of the data entries. |
||
| 112 | */ |
||
| 113 | 78 | protected function initChildren() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 127 | * |
||
| 128 | * @param array $locales |
||
| 129 | * the available locales |
||
| 130 | * @param array $crud |
||
| 131 | * the CRUD entity map |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | * the map with localized entity labels |
||
| 135 | */ |
||
| 136 | 78 | protected function getLocaleLabels(array $locales, array $crud) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Configures the EntityDefinition according to the given |
||
| 149 | * CRUD entity map. |
||
| 150 | * |
||
| 151 | * @param EntityDefinition $definition |
||
| 152 | * the definition to configure |
||
| 153 | * @param array $crud |
||
| 154 | * the CRUD entity map |
||
| 155 | */ |
||
| 156 | 78 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Creates and setups an EntityDefinition instance. |
||
| 180 | * |
||
| 181 | * @param Translator $translator |
||
| 182 | * the Translator to use for some standard field labels |
||
| 183 | * @param EntityDefinitionFactoryInterface $entityDefinitionFactory |
||
| 184 | * the EntityDefinitionFactory to use |
||
| 185 | * @param array $locales |
||
| 186 | * the available locales |
||
| 187 | * @param array $crud |
||
| 188 | * the parsed YAML of a CRUD entity |
||
| 189 | * @param string $name |
||
| 190 | * the name of the entity |
||
| 191 | * |
||
| 192 | * @return EntityDefinition |
||
| 193 | * the EntityDefinition good to go |
||
| 194 | */ |
||
| 195 | 78 | protected function createDefinition(Translator $translator, EntityDefinitionFactoryInterface $entityDefinitionFactory, array $locales, array $crud, $name) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Validates the parsed entity definition. |
||
| 219 | * |
||
| 220 | * @param Container $app |
||
| 221 | * the application container |
||
| 222 | * @param array $entityDefinition |
||
| 223 | * the entity definition to validate |
||
| 224 | */ |
||
| 225 | 78 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Initializes the instance. |
||
| 238 | * |
||
| 239 | * @param string|null $crudFileCachingDirectory |
||
| 240 | * the writable directory to store the CRUD YAML file cache |
||
| 241 | * @param Container $app |
||
| 242 | * the application container |
||
| 243 | */ |
||
| 244 | 79 | public function init($crudFileCachingDirectory, Container $app) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 268 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 269 | * |
||
| 270 | * @param Container $app |
||
| 271 | * the Container instance of the Silex application |
||
| 272 | */ |
||
| 273 | 11 | public function register(Container $app) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Initializes the crud service right after boot. |
||
| 289 | * |
||
| 290 | * @param Application $app |
||
| 291 | * the Container instance of the Silex application |
||
| 292 | */ |
||
| 293 | 79 | public function boot(Application $app) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Getter for the AbstractData instances. |
||
| 302 | * |
||
| 303 | * @param string $name |
||
| 304 | * the entity name of the desired Data instance |
||
| 305 | * |
||
| 306 | * @return AbstractData |
||
| 307 | * the AbstractData instance or null on invalid name |
||
| 308 | */ |
||
| 309 | 70 | public function getData($name) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Getter for all available entity names. |
||
| 319 | * |
||
| 320 | * @return string[] |
||
| 321 | * a list of all available entity names |
||
| 322 | */ |
||
| 323 | 7 | public function getEntities() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Getter for the entities for the navigation bar. |
||
| 330 | * |
||
| 331 | * @return string[] |
||
| 332 | * a list of all available entity names with their group |
||
| 333 | */ |
||
| 334 | 10 | public function getEntitiesNavBar() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Sets a template to use instead of the build in ones. |
||
| 350 | * |
||
| 351 | * @param string $key |
||
| 352 | * the template key to use in this format: |
||
| 353 | * $section.$action.$entity |
||
| 354 | * $section.$action |
||
| 355 | * $section |
||
| 356 | * @param string $template |
||
| 357 | * the template to use for this key |
||
| 358 | */ |
||
| 359 | 12 | public function setTemplate($key, $template) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Determines the Twig template to use for the given parameters depending on |
||
| 366 | * the existance of certain template keys set in this order: |
||
| 367 | * |
||
| 368 | * $section.$action.$entity |
||
| 369 | * $section.$action |
||
| 370 | * $section |
||
| 371 | * |
||
| 372 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 373 | * |
||
| 374 | * @param string $section |
||
| 375 | * the section of the template, either "layout" or "template" |
||
| 376 | * @param string $action |
||
| 377 | * the current calling action like "create" or "show" |
||
| 378 | * @param string $entity |
||
| 379 | * the current calling entity |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | * the best fitting template |
||
| 383 | */ |
||
| 384 | 11 | public function getTemplate($section, $action, $entity) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Sets the locale to be used. |
||
| 405 | * |
||
| 406 | * @param string $locale |
||
| 407 | * the locale to be used. |
||
| 408 | */ |
||
| 409 | 10 | public function setLocale($locale) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Gets the available locales. |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | * the available locales |
||
| 421 | */ |
||
| 422 | 79 | public function getLocales() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Gets whether CRUDlex manages the i18n. |
||
| 443 | * @return bool |
||
| 444 | * true if so |
||
| 445 | */ |
||
| 446 | 11 | public function isManageI18n() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Sets whether CRUDlex manages the i18n. |
||
| 453 | * @param bool $manageI18n |
||
| 454 | * true if so |
||
| 455 | */ |
||
| 456 | 1 | public function setManageI18n($manageI18n) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Generates an URL. |
||
| 463 | * @param string $name |
||
| 464 | * the name of the route |
||
| 465 | * @param mixed $parameters |
||
| 466 | * an array of parameters |
||
| 467 | * @return null|string |
||
| 468 | * the generated URL |
||
| 469 | */ |
||
| 470 | 11 | public function generateURL($name, $parameters) |
|
| 474 | |||
| 475 | } |
||
| 476 |