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) { |
|
| 125 | $toConfigure = [ |
||
| 126 | 78 | 'deleteCascade', |
|
| 127 | 'listFields', |
||
| 128 | 'filter', |
||
| 129 | 'childrenLabelFields', |
||
| 130 | 'pageSize', |
||
| 131 | 'initialSortField', |
||
| 132 | 'initialSortAscending', |
||
| 133 | 'navBarGroup', |
||
| 134 | 'optimisticLocking' |
||
| 135 | ]; |
||
| 136 | 78 | foreach ($toConfigure as $field) { |
|
| 137 | 78 | if (array_key_exists($field, $crud)) { |
|
| 138 | 78 | $function = 'set'.ucfirst($field); |
|
| 139 | 78 | $definition->$function($crud[$field]); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | 78 | } |
|
| 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) { |
|
| 160 | 78 | $label = array_key_exists('label', $crud) ? $crud['label'] : $name; |
|
| 161 | 78 | $localeLabels = $this->getLocaleLabels($locales, $crud); |
|
| 162 | $standardFieldLabels = [ |
||
| 163 | 78 | 'id' => $app['translator']->trans('crudlex.label.id'), |
|
| 164 | 78 | 'created_at' => $app['translator']->trans('crudlex.label.created_at'), |
|
| 165 | 78 | 'updated_at' => $app['translator']->trans('crudlex.label.updated_at') |
|
| 166 | ]; |
||
| 167 | |||
| 168 | 78 | $factory = $app->offsetExists('crud.entitydefinitionfactory') ? $app['crud.entitydefinitionfactory'] : new EntityDefinitionFactory(); |
|
| 169 | |||
| 170 | 78 | $definition = $factory->createEntityDefinition( |
|
| 171 | 78 | $crud['table'], |
|
| 172 | 78 | $crud['fields'], |
|
| 173 | $label, |
||
| 174 | $localeLabels, |
||
| 175 | $standardFieldLabels, |
||
| 176 | 78 | $this |
|
| 177 | ); |
||
| 178 | 78 | $this->configureDefinition($definition, $crud); |
|
| 179 | 78 | return $definition; |
|
| 180 | } |
||
| 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) { |
|
| 191 | 78 | $doValidate = !$app->offsetExists('crud.validateentitydefinition') || $app['crud.validateentitydefinition'] === true; |
|
| 192 | 78 | if ($doValidate) { |
|
| 193 | 77 | $validator = $app->offsetExists('crud.entitydefinitionvalidator') |
|
| 194 | 1 | ? $app['crud.entitydefinitionvalidator'] |
|
| 195 | 77 | : new EntityDefinitionValidator(); |
|
| 196 | 77 | $validator->validate($entityDefinition); |
|
| 197 | } |
||
| 198 | 78 | } |
|
| 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) { |
|
| 215 | |||
| 216 | 79 | $reader = new YamlReader($crudFileCachingDirectory); |
|
| 217 | 79 | $parsedYaml = $reader->read($crudFile); |
|
| 218 | |||
| 219 | 78 | $this->validateEntityDefinition($app, $parsedYaml); |
|
| 220 | |||
| 221 | 78 | $locales = $this->initLocales($app); |
|
| 222 | 78 | $this->datas = []; |
|
| 223 | 78 | foreach ($parsedYaml as $name => $crud) { |
|
| 224 | 78 | $definition = $this->createDefinition($app, $locales, $crud, $name); |
|
| 225 | 78 | $this->datas[$name] = $dataFactory->createData($definition, $fileProcessor); |
|
| 226 | } |
||
| 227 | |||
| 228 | 78 | $this->initChildren(); |
|
| 229 | |||
| 230 | 78 | } |
|
| 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 |