| Total Complexity | 44 |
| Total Lines | 316 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Standard |
||
| 24 | extends \Aimeos\Client\JsonApi\Base |
||
| 25 | implements \Aimeos\Client\JsonApi\Iface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Deletes the resource or the resource list |
||
| 29 | * |
||
| 30 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 31 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 32 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 33 | */ |
||
| 34 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 35 | { |
||
| 36 | $view = $this->getView(); |
||
| 37 | |||
| 38 | try |
||
| 39 | { |
||
| 40 | $body = (string) $request->getBody(); |
||
| 41 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 42 | $items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems(); |
||
| 43 | |||
| 44 | if( ( $relId = $view->param( 'relatedid' ) ) === null ) |
||
| 45 | { |
||
| 46 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 47 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 48 | } |
||
| 49 | |||
| 50 | if( !is_array( $payload->data ) ) { |
||
| 51 | $payload->data = [$payload->data]; |
||
| 52 | } |
||
| 53 | |||
| 54 | foreach( $payload->data as $entry ) |
||
| 55 | { |
||
| 56 | if( !isset( $entry->id ) ) { |
||
| 57 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
||
| 58 | } |
||
| 59 | |||
| 60 | if( ( $item = $items->get( $entry->id ) ) !== null ) { |
||
| 61 | $cntl->deletePropertyItem( $item ); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $cntl->store(); |
||
| 66 | } |
||
| 67 | else |
||
| 68 | { |
||
| 69 | if( ( $item = $items->get( $relId ) ) !== null ) { |
||
| 70 | $cntl->deletePropertyItem( $item )->store(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $status = 200; |
||
| 75 | } |
||
| 76 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 77 | { |
||
| 78 | $status = 403; |
||
| 79 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 80 | } |
||
| 81 | catch( \Aimeos\MShop\Exception $e ) |
||
| 82 | { |
||
| 83 | $status = 404; |
||
| 84 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 85 | } |
||
| 86 | catch( \Exception $e ) |
||
| 87 | { |
||
| 88 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 89 | $view->errors = $this->getErrorDetails( $e ); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->render( $response, $view, $status ); |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns the resource or the resource list |
||
| 98 | * |
||
| 99 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 100 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 101 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 102 | */ |
||
| 103 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 104 | { |
||
| 105 | $view = $this->getView(); |
||
| 106 | |||
| 107 | try |
||
| 108 | { |
||
| 109 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 110 | $items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems(); |
||
| 111 | |||
| 112 | if( ( $relId = $view->param( 'relatedid' ) ) == null ) |
||
| 113 | { |
||
| 114 | $view->items = $items; |
||
| 115 | $view->total = count( $items ); |
||
| 116 | } |
||
| 117 | else |
||
| 118 | { |
||
| 119 | $view->items = $items->get( $relId ); |
||
| 120 | $view->total = $items->isEmpty() ? 0 : 1; |
||
| 121 | } |
||
| 122 | |||
| 123 | $status = 200; |
||
| 124 | } |
||
| 125 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 126 | { |
||
| 127 | $status = 403; |
||
| 128 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 129 | } |
||
| 130 | catch( \Aimeos\MShop\Exception $e ) |
||
| 131 | { |
||
| 132 | $status = 404; |
||
| 133 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 134 | } |
||
| 135 | catch( \Exception $e ) |
||
| 136 | { |
||
| 137 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 138 | $view->errors = $this->getErrorDetails( $e ); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->render( $response, $view, $status ); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Updates the resource or the resource list partitially |
||
| 147 | * |
||
| 148 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 149 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 150 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 151 | */ |
||
| 152 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 153 | { |
||
| 154 | $view = $this->getView(); |
||
| 155 | |||
| 156 | try |
||
| 157 | { |
||
| 158 | $body = (string) $request->getBody(); |
||
| 159 | |||
| 160 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 161 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 162 | } |
||
| 163 | |||
| 164 | $status = 404; |
||
| 165 | $view->total = 0; |
||
| 166 | $relId = $view->param( 'relatedid' ); |
||
| 167 | |||
| 168 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 169 | $items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems(); |
||
| 170 | |||
| 171 | if( ( $item = $items->get( $relId ) ) !== null ) |
||
| 172 | { |
||
| 173 | $attributes = (array) $payload->data->attributes; |
||
| 174 | $propItem = $item->fromArray( $attributes ); |
||
| 175 | $cntl->addPropertyItem( $propItem )->store(); |
||
| 176 | |||
| 177 | $view->items = $propItem; |
||
| 178 | $view->total = 1; |
||
| 179 | $status = 200; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 183 | { |
||
| 184 | $status = 403; |
||
| 185 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 186 | } |
||
| 187 | catch( \Aimeos\MShop\Exception $e ) |
||
| 188 | { |
||
| 189 | $status = 404; |
||
| 190 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 191 | } |
||
| 192 | catch( \Exception $e ) |
||
| 193 | { |
||
| 194 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 195 | $view->errors = $this->getErrorDetails( $e ); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $this->render( $response, $view, $status ); |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Creates or updates the resource or the resource list |
||
| 204 | * |
||
| 205 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 206 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 207 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 208 | */ |
||
| 209 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 210 | { |
||
| 211 | $view = $this->getView(); |
||
| 212 | |||
| 213 | try |
||
| 214 | { |
||
| 215 | $body = (string) $request->getBody(); |
||
| 216 | |||
| 217 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 218 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if( !is_array( $payload->data ) ) { |
||
| 222 | $payload->data = [$payload->data]; |
||
| 223 | } |
||
| 224 | |||
| 225 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' )->uses( ['customer/property'] ); |
||
| 226 | |||
| 227 | foreach( $payload->data as $entry ) |
||
| 228 | { |
||
| 229 | if( !isset( $entry->attributes ) ) { |
||
| 230 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) ); |
||
| 231 | } |
||
| 232 | |||
| 233 | $propItem = $cntl->createPropertyItem( (array) $entry->attributes ); |
||
| 234 | $cntl->addPropertyItem( $propItem ); |
||
| 235 | } |
||
| 236 | |||
| 237 | $view->items = $cntl->store()->get()->getPropertyItems(); |
||
| 238 | $view->total = count( $view->items ); |
||
| 239 | $status = 201; |
||
| 240 | } |
||
| 241 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 242 | { |
||
| 243 | $status = 403; |
||
| 244 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 245 | } |
||
| 246 | catch( \Aimeos\MShop\Exception $e ) |
||
| 247 | { |
||
| 248 | $status = 404; |
||
| 249 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 250 | } |
||
| 251 | catch( \Exception $e ) |
||
| 252 | { |
||
| 253 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 254 | $view->errors = $this->getErrorDetails( $e ); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $this->render( $response, $view, $status ); |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the available REST verbs and the available parameters |
||
| 263 | * |
||
| 264 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 265 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 266 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 267 | */ |
||
| 268 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the response object with the rendered header and body |
||
| 302 | * |
||
| 303 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 304 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 305 | * @param int $status HTTP status code |
||
| 306 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 307 | */ |
||
| 308 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface |
||
| 339 | } |
||
| 340 | } |
||
| 341 |