| Conditions | 14 |
| Paths | 129 |
| Total Lines | 57 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 216 |