Complex classes like ResourceLoader 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 ResourceLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class ResourceLoader implements LoaderInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var RegistryInterface |
||
| 30 | */ |
||
| 31 | private $resourceRegistry; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var RouteFactoryInterface |
||
| 35 | */ |
||
| 36 | private $routeFactory; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param RegistryInterface $resourceRegistry |
||
| 40 | * @param RouteFactoryInterface $routeFactory |
||
| 41 | */ |
||
| 42 | public function __construct(RegistryInterface $resourceRegistry, RouteFactoryInterface $routeFactory) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function load($resource, $type = null): RouteCollection |
||
| 52 | { |
||
| 53 | $processor = new Processor(); |
||
| 54 | $configurationDefinition = new Configuration(); |
||
| 55 | |||
| 56 | $configuration = Yaml::parse($resource); |
||
| 57 | $configuration = $processor->processConfiguration($configurationDefinition, ['routing' => $configuration]); |
||
| 58 | |||
| 59 | if (!empty($configuration['only']) && !empty($configuration['except'])) { |
||
| 60 | throw new \InvalidArgumentException('You can configure only one of "except" & "only" options.'); |
||
| 61 | } |
||
| 62 | |||
| 63 | $routesToGenerate = ['show', 'index', 'create', 'update', 'delete']; |
||
| 64 | |||
| 65 | if (!empty($configuration['only'])) { |
||
| 66 | $routesToGenerate = $configuration['only']; |
||
| 67 | } |
||
| 68 | if (!empty($configuration['except'])) { |
||
| 69 | $routesToGenerate = array_diff($routesToGenerate, $configuration['except']); |
||
| 70 | } |
||
| 71 | |||
| 72 | $isApi = $type === 'sylius.resource_api'; |
||
| 73 | |||
| 74 | /** @var MetadataInterface $metadata */ |
||
| 75 | $metadata = $this->resourceRegistry->get($configuration['alias']); |
||
| 76 | $routes = $this->routeFactory->createRouteCollection(); |
||
| 77 | |||
| 78 | $rootPath = sprintf('/%s/', $configuration['path'] ?? Urlizer::urlize($metadata->getPluralName())); |
||
| 79 | $identifier = sprintf('{%s}', $configuration['identifier']); |
||
| 80 | |||
| 81 | if (in_array('index', $routesToGenerate, true)) { |
||
| 82 | $indexRoute = $this->createRoute($metadata, $configuration, $rootPath, 'index', ['GET'], $isApi); |
||
| 83 | $routes->add($this->getRouteName($metadata, $configuration, 'index'), $indexRoute); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (in_array('create', $routesToGenerate, true)) { |
||
| 87 | $createRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath : $rootPath . 'new', 'create', $isApi ? ['POST'] : ['GET', 'POST'], $isApi); |
||
| 88 | $routes->add($this->getRouteName($metadata, $configuration, 'create'), $createRoute); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (in_array('update', $routesToGenerate, true)) { |
||
| 92 | $updateRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath . $identifier : $rootPath . $identifier . '/edit', 'update', $isApi ? ['PUT', 'PATCH'] : ['GET', 'PUT', 'PATCH'], $isApi); |
||
| 93 | $routes->add($this->getRouteName($metadata, $configuration, 'update'), $updateRoute); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (in_array('show', $routesToGenerate, true)) { |
||
| 97 | $showRoute = $this->createRoute($metadata, $configuration, $rootPath . $identifier, 'show', ['GET'], $isApi); |
||
| 98 | $routes->add($this->getRouteName($metadata, $configuration, 'show'), $showRoute); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (in_array('delete', $routesToGenerate, true)) { |
||
| 102 | $deleteRoute = $this->createRoute($metadata, $configuration, $rootPath . $identifier, 'delete', ['DELETE'], $isApi); |
||
| 103 | $routes->add($this->getRouteName($metadata, $configuration, 'delete'), $deleteRoute); |
||
| 104 | } |
||
| 105 | |||
| 106 | return $routes; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function supports($resource, $type = null): bool |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function getResolver(): void |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function setResolver(LoaderResolverInterface $resolver): void |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param MetadataInterface $metadata |
||
| 135 | * @param array $configuration |
||
| 136 | * @param string $path |
||
| 137 | * @param string $actionName |
||
| 138 | * @param array $methods |
||
| 139 | * @param bool $isApi |
||
| 140 | * |
||
| 141 | * @return Route |
||
| 142 | */ |
||
| 143 | private function createRoute( |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param MetadataInterface $metadata |
||
| 204 | * @param array $configuration |
||
| 205 | * @param string $actionName |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | private function getRouteName(MetadataInterface $metadata, array $configuration, string $actionName): string |
||
| 215 | } |
||
| 216 |