| Conditions | 13 |
| Paths | 25 |
| Total Lines | 115 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 71 | public function getSwaggerSpec() |
||
| 72 | { |
||
| 73 | $ret = $this->getBasicStructure(); |
||
| 74 | $routingMap = $this->restUtils->getServiceRoutingMap(); |
||
| 75 | $paths = array(); |
||
| 76 | |||
| 77 | foreach ($routingMap as $contName => $routes) { |
||
| 78 | list(, $bundle,, $document) = explode('.', $contName); |
||
| 79 | |||
| 80 | /** @var Route $route */ |
||
| 81 | foreach ($routes as $routeName => $route) { |
||
| 82 | $routeMethod = strtolower($route->getMethods()[0]); |
||
| 83 | |||
| 84 | if ($routeMethod == 'options') { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | // skip /schema/ stuff |
||
| 89 | if (strpos($route->getPath(), '/schema/') !== false) { |
||
| 90 | list($pattern, $method, $data) = $this->getSchemaRoutes($route); |
||
| 91 | $paths[$pattern][$method] = $data; |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** @var \Graviton\RestBundle\Model\DocumentModel $thisModel */ |
||
| 96 | $thisModel = $this->restUtils->getModelFromRoute($route); |
||
| 97 | if ($thisModel === false) { |
||
| 98 | throw new \LogicException( |
||
| 99 | sprintf( |
||
| 100 | 'Could not resolve route "%s" to model', |
||
| 101 | $routeName |
||
| 102 | ) |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | $entityClassName = str_replace('\\', '', get_class($thisModel)); |
||
| 107 | |||
| 108 | $schema = $this->schemaUtils->getModelSchema($entityClassName, $thisModel, false); |
||
| 109 | |||
| 110 | $ret['definitions'][$entityClassName] = json_decode( |
||
| 111 | $this->restUtils->serializeContent($schema), |
||
| 112 | true |
||
| 113 | ); |
||
| 114 | |||
| 115 | $isCollectionRequest = true; |
||
| 116 | if (in_array('id', array_keys($route->getRequirements())) === true) { |
||
| 117 | $isCollectionRequest = false; |
||
| 118 | } |
||
| 119 | |||
| 120 | $thisPattern = $route->getPattern(); |
||
| 121 | $entityName = ucfirst($document); |
||
| 122 | |||
| 123 | $type = method_exists($schema->getProperty('id'), 'getType') ? |
||
| 124 | $schema->getProperty('id')->getType() : 'local'; |
||
| 125 | |||
| 126 | $thisPath = $this->getBasicPathStructure( |
||
| 127 | $isCollectionRequest, |
||
| 128 | $entityName, |
||
| 129 | $entityClassName, |
||
| 130 | $type |
||
| 131 | ); |
||
| 132 | |||
| 133 | $thisPath['tags'] = $this->getPathTags($route); |
||
| 134 | $thisPath['operationId'] = $routeName; |
||
| 135 | $thisPath['summary'] = $this->getSummary($routeMethod, $isCollectionRequest, $entityName); |
||
| 136 | |||
| 137 | // post body stuff |
||
| 138 | if ($routeMethod == 'put' || $routeMethod == 'post') { |
||
| 139 | // special handling for POST/PUT.. we need to have 2 schemas, one for response, one for request.. |
||
| 140 | // we don't want to have ID in the request body within those requests do we.. |
||
| 141 | // an exception is when id is required.. |
||
| 142 | $incomingEntitySchema = $entityClassName; |
||
| 143 | if (is_null($schema->getRequired()) || !in_array('id', $schema->getRequired())) { |
||
| 144 | $incomingEntitySchema = $incomingEntitySchema . 'Incoming'; |
||
| 145 | $incomingSchema = clone $schema; |
||
| 146 | $incomingSchema->removeProperty('id'); |
||
| 147 | $ret['definitions'][$incomingEntitySchema] = json_decode( |
||
| 148 | $this->restUtils->serializeContent($incomingSchema), |
||
| 149 | true |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | $thisPath['parameters'][] = array( |
||
| 154 | 'name' => $bundle, |
||
| 155 | 'in' => 'body', |
||
| 156 | 'description' => 'Post', |
||
| 157 | 'required' => true, |
||
| 158 | 'schema' => array('$ref' => '#/definitions/' . $incomingEntitySchema) |
||
| 159 | ); |
||
| 160 | |||
| 161 | if ($routeMethod == 'post') { |
||
| 162 | $thisPath['responses'][201] = $thisPath['responses'][200]; |
||
| 163 | unset($thisPath['responses'][200]); |
||
| 164 | } |
||
| 165 | |||
| 166 | // add error responses.. |
||
| 167 | $thisPath['responses'][400] = array( |
||
| 168 | 'description' => 'Bad request', |
||
| 169 | 'schema' => array( |
||
| 170 | 'type' => 'object' |
||
| 171 | ) |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $paths[$thisPattern][$routeMethod] = $thisPath; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | $ret['definitions']['SchemaModel'] = $this->schemaModel->getSchema(); |
||
| 180 | |||
| 181 | ksort($paths); |
||
| 182 | $ret['paths'] = $paths; |
||
| 183 | |||
| 184 | return $ret; |
||
| 185 | } |
||
| 186 | |||
| 344 |