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 |
||
| 31 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Holds the data instances. |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $datas; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initializes needed but yet missing service providers. |
||
| 42 | * |
||
| 43 | * @param Container $app |
||
| 44 | * the application container |
||
| 45 | */ |
||
| 46 | 76 | protected function initMissingServiceProviders(Container $app) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Initializes the available locales. |
||
| 68 | * |
||
| 69 | * @param Container $app |
||
| 70 | * the application container |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | * the available locales |
||
| 74 | */ |
||
| 75 | 75 | protected function initLocales(Container $app) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Initializes the children of the data entries. |
||
| 88 | */ |
||
| 89 | 75 | protected function initChildren() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 103 | * |
||
| 104 | * @param array $locales |
||
| 105 | * the available locales |
||
| 106 | * @param array $crud |
||
| 107 | * the CRUD entity map |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | * the map with localized entity labels |
||
| 111 | */ |
||
| 112 | 75 | protected function getLocaleLabels(array $locales, array $crud) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Configures the EntityDefinition according to the given |
||
| 125 | * CRUD entity map. |
||
| 126 | * |
||
| 127 | * @param EntityDefinition $definition |
||
| 128 | * the definition to configure |
||
| 129 | * @param array $crud |
||
| 130 | * the CRUD entity map |
||
| 131 | */ |
||
| 132 | 75 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Creates and setups an EntityDefinition instance. |
||
| 155 | * |
||
| 156 | * @param Container $app |
||
| 157 | * the application container |
||
| 158 | * @param array $locales |
||
| 159 | * the available locales |
||
| 160 | * @param array $crud |
||
| 161 | * the parsed YAML of a CRUD entity |
||
| 162 | * @param string $name |
||
| 163 | * the name of the entity |
||
| 164 | * |
||
| 165 | * @return EntityDefinition |
||
| 166 | * the EntityDefinition good to go |
||
| 167 | */ |
||
| 168 | 75 | protected function createDefinition(Container $app, array $locales, array $crud, $name) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Validates the parsed entity definition. |
||
| 194 | * |
||
| 195 | * @param Container $app |
||
| 196 | * the application container |
||
| 197 | * @param array $entityDefinition |
||
| 198 | * the entity definition to validate |
||
| 199 | */ |
||
| 200 | 75 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Initializes the instance. |
||
| 213 | * |
||
| 214 | * @param string|null $crudFileCachingDirectory |
||
| 215 | * the writable directory to store the CRUD YAML file cache |
||
| 216 | * @param Container $app |
||
| 217 | * the application container |
||
| 218 | */ |
||
| 219 | 76 | public function init($crudFileCachingDirectory, Container $app) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 240 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 241 | * |
||
| 242 | * @param Container $app |
||
| 243 | * the Container instance of the Silex application |
||
| 244 | */ |
||
| 245 | 11 | public function register(Container $app) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Initializes the crud service right after boot. |
||
| 260 | * |
||
| 261 | * @param Application $app |
||
| 262 | * the Container instance of the Silex application |
||
| 263 | */ |
||
| 264 | 76 | public function boot(Application $app) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Getter for the AbstractData instances. |
||
| 273 | * |
||
| 274 | * @param string $name |
||
| 275 | * the entity name of the desired Data instance |
||
| 276 | * |
||
| 277 | * @return AbstractData |
||
| 278 | * the AbstractData instance or null on invalid name |
||
| 279 | */ |
||
| 280 | 69 | public function getData($name) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Getter for all available entity names. |
||
| 290 | * |
||
| 291 | * @return string[] |
||
| 292 | * a list of all available entity names |
||
| 293 | */ |
||
| 294 | 3 | public function getEntities() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Getter for the entities for the navigation bar. |
||
| 301 | * |
||
| 302 | * @return string[] |
||
| 303 | * a list of all available entity names with their group |
||
| 304 | */ |
||
| 305 | 10 | public function getEntitiesNavBar() |
|
| 306 | { |
||
| 307 | 10 | $result = []; |
|
| 308 | 10 | foreach ($this->datas as $entity => $data) { |
|
| 309 | 10 | $navBarGroup = $data->getDefinition()->getNavBarGroup(); |
|
| 310 | 10 | if ($navBarGroup !== 'main') { |
|
| 311 | 10 | $result[$navBarGroup][] = $entity; |
|
| 312 | } else { |
||
| 313 | 10 | $result[$entity] = 'main'; |
|
| 314 | } |
||
| 315 | } |
||
| 316 | 10 | return $result; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Determines the Twig template to use for the given parameters depending on |
||
| 321 | * the existance of certain keys in the Container $app in this order: |
||
| 322 | * |
||
| 323 | * crud.$section.$action.$entity |
||
| 324 | * crud.$section.$action |
||
| 325 | * crud.$section |
||
| 326 | * |
||
| 327 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 328 | * |
||
| 329 | * @param Container $app |
||
| 330 | * the Silex application |
||
| 331 | * @param string $section |
||
| 332 | * the section of the template, either "layout" or "template" |
||
| 333 | * @param string $action |
||
| 334 | * the current calling action like "create" or "show" |
||
| 335 | * @param string $entity |
||
| 336 | * the current calling entity |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | * the best fitting template |
||
| 340 | */ |
||
| 341 | 11 | public function getTemplate(Container $app, $section, $action, $entity) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Sets the locale to be used. |
||
| 363 | * |
||
| 364 | * @param string $locale |
||
| 365 | * the locale to be used. |
||
| 366 | */ |
||
| 367 | 10 | public function setLocale($locale) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Gets the available locales. |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | * the available locales |
||
| 379 | */ |
||
| 380 | 76 | public function getLocales() |
|
| 398 | |||
| 399 | } |
||
| 400 |