Conditions | 12 |
Paths | 120 |
Total Lines | 88 |
Code Lines | 39 |
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 | $view = $this->getView(); |
||
122 | |||
123 | try |
||
124 | { |
||
125 | $body = (string) $request->getBody(); |
||
126 | $relId = $view->param( 'relatedid' ); |
||
127 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
128 | |||
129 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !isset( $payload->data->attributes ) ) { |
||
130 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
131 | } |
||
132 | |||
133 | if( !is_array( $payload->data ) ) { |
||
134 | $payload->data = [$payload->data]; |
||
135 | } |
||
136 | |||
137 | foreach( $payload->data as $entry ) |
||
138 | { |
||
139 | if( $relId !== '' && $relId !== null ) { |
||
140 | $entry->id = $relId; |
||
141 | } |
||
142 | |||
143 | if( !isset( $entry->id ) ) { |
||
144 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) ); |
||
145 | } |
||
146 | |||
147 | $qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
||
148 | $cfgAttrCodes = ( isset( $entry->attributes->codes ) ? (array) $entry->attributes->codes : [] ); |
||
149 | |||
150 | $this->controller->editProduct( $entry->id, $qty, $cfgAttrCodes ); |
||
151 | } |
||
152 | |||
153 | $view->item = $this->controller->get(); |
||
154 | $status = 200; |
||
155 | } |
||
156 | catch( \Aimeos\MShop\Exception $e ) |
||
157 | { |
||
158 | $status = 404; |
||
159 | $view->errors = array( array( |
||
160 | 'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
||
161 | 'detail' => $e->getTraceAsString(), |
||
162 | ) ); |
||
163 | } |
||
164 | catch( \Exception $e ) |
||
165 | { |
||
166 | $status = 500; |
||
167 | $view->errors = array( array( |
||
168 | 'title' => $e->getMessage(), |
||
169 | 'detail' => $e->getTraceAsString(), |
||
170 | ) ); |
||
171 | } |
||
172 | |||
173 | return $this->render( $response, $view, $status ); |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Creates or updates the resource or the resource list |
||
179 | * |
||
180 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
181 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
182 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
183 | */ |
||
184 | public function post( ServerRequestInterface $request, ResponseInterface $response ) |
||
185 | { |
||
186 | $view = $this->getView(); |
||
187 | |||
188 | try |
||
189 | { |
||
190 | $body = (string) $request->getBody(); |
||
191 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
192 | |||
193 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
194 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
195 | } |
||
196 | |||
197 | if( !is_array( $payload->data ) ) { |
||
198 | $payload->data = [$payload->data]; |
||
199 | } |
||
200 | |||
201 | foreach( $payload->data as $entry ) |
||
202 | { |
||
203 | if( !isset( $entry->attributes ) || !isset( $entry->attributes->productid ) ) { |
||
204 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Product ID is missing' ) ); |
||
205 | } |
||
206 | |||
207 | $qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
||
208 | $stocktype = ( isset( $entry->attributes->stocktype ) ? $entry->attributes->stocktype : 'default' ); |
||
209 | $variantAttrIds = ( isset( $entry->attributes->variant ) ? (array) $entry->attributes->variant : [] ); |
||
264 |