Conditions | 12 |
Paths | 15 |
Total Lines | 112 |
Code Lines | 65 |
Lines | 0 |
Ratio | 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 | $thisPath = $this->getBasicPathStructure( |
||
124 | $isCollectionRequest, |
||
125 | $entityName, |
||
126 | $entityClassName, |
||
127 | $schema->getProperty('id')->getType() |
||
128 | ); |
||
129 | |||
130 | $thisPath['tags'] = $this->getPathTags($route); |
||
131 | $thisPath['operationId'] = $routeName; |
||
132 | $thisPath['summary'] = $this->getSummary($routeMethod, $isCollectionRequest, $entityName); |
||
133 | |||
134 | // post body stuff |
||
135 | if ($routeMethod == 'put' || $routeMethod == 'post') { |
||
136 | // special handling for POST/PUT.. we need to have 2 schemas, one for response, one for request.. |
||
137 | // we don't want to have ID in the request body within those requests do we.. |
||
138 | // an exception is when id is required.. |
||
139 | $incomingEntitySchema = $entityClassName; |
||
140 | if (is_null($schema->getRequired()) || !in_array('id', $schema->getRequired())) { |
||
141 | $incomingEntitySchema = $incomingEntitySchema . 'Incoming'; |
||
142 | $incomingSchema = clone $schema; |
||
143 | $incomingSchema->removeProperty('id'); |
||
144 | $ret['definitions'][$incomingEntitySchema] = json_decode( |
||
145 | $this->restUtils->serializeContent($incomingSchema), |
||
146 | true |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | $thisPath['parameters'][] = array( |
||
151 | 'name' => $bundle, |
||
152 | 'in' => 'body', |
||
153 | 'description' => 'Post', |
||
154 | 'required' => true, |
||
155 | 'schema' => array('$ref' => '#/definitions/' . $incomingEntitySchema) |
||
156 | ); |
||
157 | |||
158 | if ($routeMethod == 'post') { |
||
159 | $thisPath['responses'][201] = $thisPath['responses'][200]; |
||
160 | unset($thisPath['responses'][200]); |
||
161 | } |
||
162 | |||
163 | // add error responses.. |
||
164 | $thisPath['responses'][400] = array( |
||
165 | 'description' => 'Bad request', |
||
166 | 'schema' => array( |
||
167 | 'type' => 'object' |
||
168 | ) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | $paths[$thisPattern][$routeMethod] = $thisPath; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $ret['definitions']['SchemaModel'] = $this->schemaModel->getSchema(); |
||
177 | |||
178 | ksort($paths); |
||
179 | $ret['paths'] = $paths; |
||
180 | |||
181 | return $ret; |
||
182 | } |
||
183 | |||
341 |