Conditions | 10 |
Paths | 40 |
Total Lines | 62 |
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 |
||
121 | public function post( ServerRequestInterface $request, ResponseInterface $response ) |
||
122 | { |
||
123 | $view = $this->getView(); |
||
124 | |||
125 | try |
||
126 | { |
||
127 | $body = (string) $request->getBody(); |
||
128 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
129 | |||
130 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
131 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
132 | } |
||
133 | |||
134 | if( !is_array( $payload->data ) ) { |
||
135 | $payload->data = [$payload->data]; |
||
136 | } |
||
137 | |||
138 | foreach( $payload->data as $entry ) |
||
139 | { |
||
140 | if( !isset( $entry->id ) ) { |
||
141 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service type (ID) is missing' ) ); |
||
142 | } |
||
143 | |||
144 | if( !isset( $entry->attributes ) ) { |
||
145 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service attributes are missing' ) ); |
||
146 | } |
||
147 | |||
148 | if( !isset( $entry->attributes->{'service.id'} ) ) { |
||
149 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service ID in attributes is missing' ) ); |
||
150 | } |
||
151 | |||
152 | $serviceId = $entry->attributes->{'service.id'}; |
||
153 | unset( $entry->attributes->{'service.id'} ); |
||
154 | |||
155 | $this->controller->setService( $entry->id, $serviceId, (array) $entry->attributes ); |
||
156 | } |
||
157 | |||
158 | |||
159 | $view->items = $this->controller->get(); |
||
160 | $view->total = 1; |
||
161 | |||
162 | $status = 201; |
||
163 | } |
||
164 | catch( \Aimeos\MShop\Exception $e ) |
||
165 | { |
||
166 | $status = 404; |
||
167 | $view->errors = array( array( |
||
168 | 'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
||
169 | 'detail' => $e->getTraceAsString(), |
||
170 | ) ); |
||
171 | } |
||
172 | catch( \Exception $e ) |
||
173 | { |
||
174 | $status = 500; |
||
175 | $view->errors = array( array( |
||
176 | 'title' => $e->getMessage(), |
||
177 | 'detail' => $e->getTraceAsString(), |
||
178 | ) ); |
||
179 | } |
||
180 | |||
181 | return $this->render( $response, $view, $status ); |
||
182 | } |
||
183 | |||
206 |