| Total Complexity | 47 |
| Total Lines | 419 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Base |
||
| 21 | { |
||
| 22 | private $context; |
||
| 23 | private $aimeos; |
||
| 24 | private $view; |
||
| 25 | private $path; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Initializes the client |
||
| 30 | * |
||
| 31 | * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
||
| 32 | * @param string $path Name of the client separated by slashes, e.g "product/property" |
||
| 33 | */ |
||
| 34 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, string $path ) |
||
| 35 | { |
||
| 36 | $this->context = $context; |
||
| 37 | $this->path = $path; |
||
| 38 | } |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Catch unknown methods |
||
| 43 | * |
||
| 44 | * @param string $name Name of the method |
||
| 45 | * @param array $param List of method parameter |
||
| 46 | * @throws \Aimeos\Admin\JsonAdm\Exception If method call failed |
||
| 47 | */ |
||
| 48 | public function __call( string $name, array $param ) |
||
| 49 | { |
||
| 50 | throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Unable to call method "%1$s"', $name ) ); |
||
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the Aimeos bootstrap object |
||
| 56 | * |
||
| 57 | * @return \Aimeos\Bootstrap The Aimeos bootstrap object |
||
| 58 | */ |
||
| 59 | public function getAimeos() : \Aimeos\Bootstrap |
||
| 60 | { |
||
| 61 | if( !isset( $this->aimeos ) ) { |
||
| 62 | throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Aimeos object not available' ) ); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $this->aimeos; |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the Aimeos bootstrap object |
||
| 71 | * |
||
| 72 | * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object |
||
| 73 | * @return \Aimeos\Admin\JsonAdm\Iface Reference to this object for fluent calls |
||
| 74 | */ |
||
| 75 | public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JsonAdm\Iface |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the view object that will generate the admin output. |
||
| 84 | * |
||
| 85 | * @return \Aimeos\MW\View\Iface The view object which generates the admin output |
||
| 86 | */ |
||
| 87 | public function getView() : \Aimeos\MW\View\Iface |
||
| 88 | { |
||
| 89 | if( !isset( $this->view ) ) { |
||
| 90 | throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'No view available' ) ); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->view; |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Sets the view object that will generate the admin output. |
||
| 99 | * |
||
| 100 | * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
||
| 101 | * @return \Aimeos\Admin\JsonAdm\Iface Reference to this object for fluent calls |
||
| 102 | */ |
||
| 103 | public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Admin\JsonAdm\Iface |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the items with parent/child relationships |
||
| 112 | * |
||
| 113 | * @param \Aimeos\Map $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 114 | * @param array $include List of resource types that should be fetched |
||
| 115 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 116 | */ |
||
| 117 | protected function getChildItems( \Aimeos\Map $items, array $include ) : \Aimeos\Map |
||
| 118 | { |
||
| 119 | return map(); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the context item object |
||
| 125 | * |
||
| 126 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 127 | */ |
||
| 128 | protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the list of domains that are available as resources |
||
| 136 | * |
||
| 137 | * @param \Aimeos\MW\View\Iface $view View object with "resource" parameter |
||
| 138 | * @return array List of domain names |
||
| 139 | */ |
||
| 140 | protected function getDomains( \Aimeos\MW\View\Iface $view ) : array |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the list items for association relationships |
||
| 170 | * |
||
| 171 | * @param \Aimeos\Map $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 172 | * @param array $include List of resource types that should be fetched |
||
| 173 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 174 | */ |
||
| 175 | protected function getListItems( \Aimeos\Map $items, array $include ) : \Aimeos\Map |
||
| 176 | { |
||
| 177 | return map(); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the path to the client |
||
| 183 | * |
||
| 184 | * @return string Client path, e.g. "product/property" |
||
| 185 | */ |
||
| 186 | protected function getPath() : string |
||
| 187 | { |
||
| 188 | return $this->path; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the items associated via a lists table |
||
| 194 | * |
||
| 195 | * @param \Aimeos\Map $listItems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 196 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 197 | */ |
||
| 198 | protected function getRefItems( \Aimeos\Map $listItems ) : \Aimeos\Map |
||
| 199 | { |
||
| 200 | $map = []; |
||
| 201 | $list = map(); |
||
| 202 | $context = $this->getContext(); |
||
| 203 | |||
| 204 | foreach( $listItems as $listItem ) { |
||
| 205 | $map[$listItem->getDomain()][] = $listItem->getRefId(); |
||
| 206 | } |
||
| 207 | |||
| 208 | foreach( $map as $domain => $ids ) |
||
| 209 | { |
||
| 210 | $manager = \Aimeos\MShop::create( $context, $domain ); |
||
| 211 | |||
| 212 | $search = $manager->createSearch(); |
||
| 213 | $search->setConditions( $search->compare( '==', str_replace( '/', '.', $domain ) . '.id', $ids ) ); |
||
| 214 | |||
| 215 | $list = $list->merge( $manager->searchItems( $search ) ); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $list; |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the list of allowed resources |
||
| 224 | * |
||
| 225 | * @param \Aimeos\MW\View\Iface $view View object with "access" helper |
||
| 226 | * @param array List of all available resources |
||
|
|
|||
| 227 | * @return array List of allowed resources |
||
| 228 | */ |
||
| 229 | protected function getAllowedResources( \Aimeos\MW\View\Iface $view, array $resources ) : array |
||
| 230 | { |
||
| 231 | $config = $this->getContext()->getConfig(); |
||
| 232 | $allowed = []; |
||
| 233 | |||
| 234 | foreach( $resources as $resource ) |
||
| 235 | { |
||
| 236 | if( $view->access( $config->get( 'admin/jsonadm/resource/' . $resource . '/groups', [] ) ) === true ) { |
||
| 237 | $allowed[] = $resource; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | return $allowed; |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the list of additional resources |
||
| 247 | * |
||
| 248 | * @param \Aimeos\MW\View\Iface $view View object with "resource" parameter |
||
| 249 | * @return array List of domain names |
||
| 250 | */ |
||
| 251 | protected function getResources( \Aimeos\MW\View\Iface $view ) : array |
||
| 252 | { |
||
| 253 | /** admin/jsonadm/resources |
||
| 254 | * A list of additional resources name whose clients are available for the JSON API |
||
| 255 | * |
||
| 256 | * The HTTP OPTIONS method returns a list of resources known by the |
||
| 257 | * JSON API including their URLs. The list of available resources |
||
| 258 | * can be exteded dynamically be implementing a new Jsonadm client |
||
| 259 | * class handling request for this new domain. |
||
| 260 | * |
||
| 261 | * The resource config lists the resources that are not automatically |
||
| 262 | * derived from the admin/jsonadm/domains configuration. |
||
| 263 | * |
||
| 264 | * @param array List of domain names |
||
| 265 | * @since 2017.07 |
||
| 266 | * @category Developer |
||
| 267 | * @see admin/jsonadm/domains |
||
| 268 | */ |
||
| 269 | return (array) $view->config( 'admin/jsonadm/resources', [] ); |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Initializes the criteria object based on the given parameter |
||
| 275 | * |
||
| 276 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 277 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 278 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 279 | */ |
||
| 280 | protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
||
| 281 | { |
||
| 282 | $this->initCriteriaConditions( $criteria, $params ); |
||
| 283 | $this->initCriteriaSortations( $criteria, $params ); |
||
| 284 | $this->initCriteriaSlice( $criteria, $params ); |
||
| 285 | |||
| 286 | return $criteria; |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Initializes the criteria object with conditions based on the given parameter |
||
| 292 | * |
||
| 293 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 294 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 295 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 296 | */ |
||
| 297 | protected function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
||
| 298 | { |
||
| 299 | if( !isset( $params['filter'] ) ) { |
||
| 300 | return $criteria; |
||
| 301 | } |
||
| 302 | |||
| 303 | if( ( $cond = $criteria->toConditions( (array) $params['filter'] ) ) !== null ) { |
||
| 304 | return $criteria->setConditions( $criteria->combine( '&&', [$cond, $criteria->getConditions()] ) ); |
||
| 305 | } |
||
| 306 | |||
| 307 | return $criteria; |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Initializes the criteria object with the slice based on the given parameter. |
||
| 313 | * |
||
| 314 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 315 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 316 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 317 | */ |
||
| 318 | protected function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
||
| 319 | { |
||
| 320 | $start = ( isset( $params['page']['offset'] ) ? (int) $params['page']['offset'] : 0 ); |
||
| 321 | $size = ( isset( $params['page']['limit'] ) ? (int) $params['page']['limit'] : 25 ); |
||
| 322 | |||
| 323 | return $criteria->setSlice( $start, $size ); |
||
| 324 | } |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Initializes the criteria object with sortations based on the given parameter |
||
| 329 | * |
||
| 330 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 331 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 332 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 333 | */ |
||
| 334 | protected function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Creates of updates several items at once |
||
| 357 | * |
||
| 358 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 359 | * @param \stdClass $request Object with request body data |
||
| 360 | * @return \Aimeos\Map List of items |
||
| 361 | */ |
||
| 362 | protected function saveData( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $request ) : \Aimeos\Map |
||
| 363 | { |
||
| 364 | $data = []; |
||
| 365 | |||
| 366 | if( isset( $request->data ) ) |
||
| 367 | { |
||
| 368 | foreach( (array) $request->data as $entry ) { |
||
| 369 | $data[] = $this->saveEntry( $manager, $entry ); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | return map( $data ); |
||
| 374 | } |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Saves and returns the new or updated item |
||
| 379 | * |
||
| 380 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 381 | * @param \stdClass $entry Object including "id" and "attributes" elements |
||
| 382 | * @return \Aimeos\MShop\Common\Item\Iface New or updated item |
||
| 383 | */ |
||
| 384 | protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) : \Aimeos\MShop\Common\Item\Iface |
||
| 403 | } |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * Saves the item references associated via the list |
||
| 408 | * |
||
| 409 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 410 | * @param \Aimeos\MShop\Common\Item\Iface $item Domain item with an unique ID set |
||
| 411 | * @param \stdClass $relationships Object including the <domain>/data/attributes structure |
||
| 412 | */ |
||
| 413 | protected function saveRelationships( \Aimeos\MShop\Common\Manager\Iface $manager, |
||
| 439 | } |
||
| 440 | } |
||
| 441 | } |
||
| 442 | } |
||
| 444 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths