| Total Complexity | 48 |
| Total Lines | 496 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SchemaComponent 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 SchemaComponent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class SchemaComponent extends Component |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @inheritDoc |
||
| 33 | */ |
||
| 34 | public $components = ['Flash']; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Cache config name for type schemas. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public const CACHE_CONFIG = '_schema_types_'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritDoc |
||
| 45 | */ |
||
| 46 | protected $_defaultConfig = [ |
||
| 47 | 'type' => null, // resource or object type name |
||
| 48 | 'internalSchema' => false, // use internal schema |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Read type JSON Schema from API using internal cache. |
||
| 53 | * |
||
| 54 | * @param string|null $type Type to get schema for. By default, configured type is used. |
||
| 55 | * @param string|null $revision Schema revision. |
||
| 56 | * @return array|bool JSON Schema. |
||
| 57 | */ |
||
| 58 | public function getSchema(?string $type = null, ?string $revision = null) |
||
| 59 | { |
||
| 60 | if ($type === null) { |
||
| 61 | $type = $this->getConfig('type'); |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($this->getConfig('internalSchema')) { |
||
| 65 | return $this->loadInternalSchema($type); |
||
| 66 | } |
||
| 67 | |||
| 68 | $schema = $this->loadWithRevision($type, $revision); |
||
| 69 | if (!empty($schema)) { |
||
| 70 | return $schema; |
||
| 71 | } |
||
| 72 | |||
| 73 | try { |
||
| 74 | $schema = Cache::remember( |
||
| 75 | CacheTools::cacheKey($type), |
||
| 76 | function () use ($type) { |
||
| 77 | return $this->fetchSchema($type); |
||
| 78 | }, |
||
| 79 | self::CACHE_CONFIG |
||
| 80 | ); |
||
| 81 | } catch (BEditaClientException $e) { |
||
| 82 | // Something bad happened. Booleans **ARE** valid JSON Schemas: returning `false` instead. |
||
| 83 | // The exception is being caught _outside_ of `Cache::remember()` to avoid caching the fallback. |
||
| 84 | $this->log($e->getMessage(), LogLevel::ERROR); |
||
| 85 | |||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $schema; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get schemas by types and return them group by type |
||
| 94 | * |
||
| 95 | * @param array $types The types |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function getSchemasByType(array $types): array |
||
| 99 | { |
||
| 100 | $schemas = []; |
||
| 101 | foreach ($types as $type) { |
||
| 102 | $schemas[$type] = $this->getSchema($type); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $schemas; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Load schema from cache with revision check. |
||
| 110 | * If cached revision don't match cache is removed. |
||
| 111 | * |
||
| 112 | * @param string $type Type to get schema for. By default, configured type is used. |
||
| 113 | * @param string|null $revision Schema revision. |
||
| 114 | * @return array|null Cached schema if revision match, null otherwise |
||
| 115 | */ |
||
| 116 | protected function loadWithRevision(string $type, ?string $revision = null): ?array |
||
| 117 | { |
||
| 118 | $key = CacheTools::cacheKey($type); |
||
| 119 | $schema = Cache::read($key, self::CACHE_CONFIG); |
||
| 120 | if (empty($schema)) { |
||
| 121 | return null; |
||
| 122 | } |
||
| 123 | $cacheRevision = empty($schema['revision']) ? null : $schema['revision']; |
||
| 124 | if ($revision === null || $cacheRevision === $revision) { |
||
| 125 | return $schema; |
||
| 126 | } |
||
| 127 | // remove from cache if revision don't match |
||
| 128 | Cache::delete($key, self::CACHE_CONFIG); |
||
| 129 | |||
| 130 | return null; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Fetch JSON Schema via API. |
||
| 135 | * |
||
| 136 | * @param string $type Type to get schema for. |
||
| 137 | * @return array|bool JSON Schema. |
||
| 138 | */ |
||
| 139 | protected function fetchSchema(string $type) |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Fetch `roles` names |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | protected function fetchRoles(): array |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Check if tags are in use |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function tagsInUse(): bool |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Fetch object type metadata |
||
| 189 | * |
||
| 190 | * @param string $type Object type. |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | protected function fetchObjectTypeMeta(string $type): array |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Fetch `categories` |
||
| 211 | * This should be called only for types having `"Categories"` association |
||
| 212 | * |
||
| 213 | * @param string $type Object type name |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | protected function fetchCategories(string $type): array |
||
| 217 | { |
||
| 218 | $data = []; |
||
| 219 | $url = sprintf('/model/categories?filter[type]=%s', $type); |
||
| 220 | $pageCount = $page = 1; |
||
| 221 | $pageSize = 100; |
||
| 222 | $query = ['page_size' => $pageSize]; |
||
| 223 | try { |
||
| 224 | while ($page <= $pageCount) { |
||
| 225 | $response = (array)ApiClientProvider::getApiClient()->get($url, $query + compact('page')); |
||
| 226 | $categories = (array)Hash::get($response, 'data'); |
||
| 227 | $data = array_merge($data, $categories); |
||
| 228 | $pageCount = (int)Hash::get($response, 'meta.pagination.page_count'); |
||
| 229 | $page++; |
||
| 230 | } |
||
| 231 | } catch (BEditaClientException $ex) { |
||
| 232 | // we ignore filter errors for now |
||
| 233 | $data = []; |
||
| 234 | } |
||
| 235 | |||
| 236 | return array_map( |
||
| 237 | function ($item) { |
||
| 238 | return [ |
||
| 239 | 'id' => Hash::get((array)$item, 'id'), |
||
| 240 | 'name' => Hash::get((array)$item, 'attributes.name'), |
||
| 241 | 'label' => Hash::get((array)$item, 'attributes.label'), |
||
| 242 | 'parent_id' => Hash::get((array)$item, 'attributes.parent_id'), |
||
| 243 | 'enabled' => Hash::get((array)$item, 'attributes.enabled'), |
||
| 244 | ]; |
||
| 245 | }, |
||
| 246 | $data |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Load internal schema properties from configuration. |
||
| 252 | * |
||
| 253 | * @param string $type Resource type name |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function loadInternalSchema(string $type): array |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Read relations schema from API using internal cache. |
||
| 266 | * |
||
| 267 | * @return array Relations schema. |
||
| 268 | */ |
||
| 269 | public function getRelationsSchema(): array |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Fetch relations schema via API. |
||
| 291 | * |
||
| 292 | * @return array Relations schema. |
||
| 293 | */ |
||
| 294 | protected function fetchRelationData(): array |
||
| 295 | { |
||
| 296 | $query = [ |
||
| 297 | 'include' => 'left_object_types,right_object_types', |
||
| 298 | 'page_size' => 100, |
||
| 299 | ]; |
||
| 300 | $response = ApiClientProvider::getApiClient()->get('/model/relations', $query); |
||
| 301 | |||
| 302 | $relations = []; |
||
| 303 | // retrieve relation right and left object types |
||
| 304 | $typeNames = Hash::combine((array)$response, 'included.{n}.id', 'included.{n}.attributes.name'); |
||
| 305 | $descendants = (array)Hash::get($this->objectTypesFeatures(), 'descendants'); |
||
| 306 | |||
| 307 | foreach ($response['data'] as $res) { |
||
| 308 | $left = (array)Hash::extract($res, 'relationships.left_object_types.data.{n}.id'); |
||
| 309 | $types = array_intersect_key($typeNames, array_flip($left)); |
||
| 310 | $left = $this->concreteTypes($types, $descendants); |
||
| 311 | |||
| 312 | $right = (array)Hash::extract($res, 'relationships.right_object_types.data.{n}.id'); |
||
| 313 | $types = array_intersect_key($typeNames, array_flip($right)); |
||
| 314 | $right = $this->concreteTypes($types, $descendants); |
||
| 315 | |||
| 316 | unset($res['relationships'], $res['links']); |
||
| 317 | $relations[$res['attributes']['name']] = $res + compact('left', 'right'); |
||
| 318 | $relations[$res['attributes']['inverse_name']] = $res + [ |
||
| 319 | 'left' => $right, |
||
| 320 | 'right' => $left, |
||
| 321 | ]; |
||
| 322 | } |
||
| 323 | Configure::load('relations'); |
||
| 324 | $schema = $relations + Configure::read('DefaultRelations'); |
||
| 325 | if (Configure::read('ChildrenParams')) { |
||
| 326 | $schema['children']['attributes']['params'] = Configure::read('ChildrenParams'); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $schema; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Retrieve concrete types from types list using `descendants` array |
||
| 334 | * |
||
| 335 | * @param array $types Object types |
||
| 336 | * @param array $descendants Descendants array |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | protected function concreteTypes(array $types, array $descendants): array |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Retrieve concrete type descendants of an object $type if any. |
||
| 356 | * |
||
| 357 | * @param string $type Object type name. |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function descendants(string $type): array |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Retrieve list of custom properties for a given object type. |
||
| 369 | * |
||
| 370 | * @param string $type Object type name. |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function customProps(string $type): array |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Fetch custom properties for a given object type. |
||
| 386 | * |
||
| 387 | * @param string $type Object type name. |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | protected function fetchCustomProps(string $type): array |
||
| 391 | { |
||
| 392 | if ($this->getConfig('internalSchema')) { |
||
| 393 | return []; // internal resources don't have custom properties |
||
| 394 | } |
||
| 395 | $query = [ |
||
| 396 | 'fields' => 'name', |
||
| 397 | 'filter' => ['object_type' => $type, 'type' => 'dynamic'], |
||
| 398 | 'page_size' => 100, |
||
| 399 | ]; |
||
| 400 | $response = ApiClientProvider::getApiClient()->get('/model/properties', $query); |
||
| 401 | |||
| 402 | return (array)Hash::extract($response, 'data.{n}.attributes.name'); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Read object types features from API |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function objectTypesFeatures(): array |
||
| 411 | { |
||
| 412 | try { |
||
| 413 | $features = (array)Cache::remember( |
||
| 414 | CacheTools::cacheKey('types_features'), |
||
| 415 | function () { |
||
| 416 | return $this->fetchObjectTypesFeatures(); |
||
| 417 | }, |
||
| 418 | self::CACHE_CONFIG |
||
| 419 | ); |
||
| 420 | } catch (BEditaClientException $e) { |
||
| 421 | $this->log($e->getMessage(), LogLevel::ERROR); |
||
| 422 | |||
| 423 | return []; |
||
| 424 | } |
||
| 425 | |||
| 426 | return $features; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Fetch object types information via API and manipulate response array. |
||
| 431 | * |
||
| 432 | * Resulting array will contain: |
||
| 433 | * * `descendants` - associative array having abstract types as keys |
||
| 434 | * and all concrete descendant types list as value |
||
| 435 | * * `uploadable` - list of concrete types having "Streams" associated, |
||
| 436 | * types that can be instantiated via file upload (like images, files) |
||
| 437 | * * `categorized` - list of concrete types having "Categories" associated |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function fetchObjectTypesFeatures(): array |
||
| 442 | { |
||
| 443 | $query = [ |
||
| 444 | 'page_size' => 100, |
||
| 445 | 'fields' => 'name,is_abstract,associations,parent_name', |
||
| 446 | 'filter' => ['enabled' => true], |
||
| 447 | ]; |
||
| 448 | $response = (array)ApiClientProvider::getApiClient()->get('/model/object_types', $query); |
||
| 449 | |||
| 450 | $descendants = (array)Hash::extract($response, 'data.{n}.attributes.parent_name'); |
||
| 451 | $descendants = array_filter(array_unique($descendants)); |
||
| 452 | $types = Hash::combine($response, 'data.{n}.attributes.name', 'data.{n}.attributes'); |
||
| 453 | $descendants = array_fill_keys($descendants, []); |
||
| 454 | $uploadable = $categorized = $tagged = []; |
||
| 455 | foreach ($types as $name => $data) { |
||
| 456 | $abstract = (bool)Hash::get($data, 'is_abstract'); |
||
| 457 | if ($abstract) { |
||
| 458 | continue; |
||
| 459 | } |
||
| 460 | $parent = (string)Hash::get($data, 'parent_name'); |
||
| 461 | $this->setDescendant($name, $parent, $types, $descendants); |
||
| 462 | if (!(bool)Hash::get($types, $name . '.is_abstract')) { |
||
| 463 | $assoc = (array)Hash::get($types, $name . '.associations'); |
||
| 464 | if (in_array('Streams', $assoc)) { |
||
| 465 | $uploadable[] = $name; |
||
| 466 | } |
||
| 467 | if (in_array('Categories', $assoc)) { |
||
| 468 | $categorized[] = $name; |
||
| 469 | } |
||
| 470 | if (in_array('Tags', $assoc)) { |
||
| 471 | $tagged[] = $name; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | } |
||
| 475 | sort($categorized); |
||
| 476 | sort($tagged); |
||
| 477 | sort($uploadable); |
||
| 478 | |||
| 479 | return compact('descendants', 'uploadable', 'categorized', 'tagged'); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set descendant in $descendants array |
||
| 484 | * |
||
| 485 | * @param string $name Object type name |
||
| 486 | * @param string $parent Parent type name |
||
| 487 | * @param array $types Types array |
||
| 488 | * @param array $descendants Descendants array |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | protected function setDescendant(string $name, string $parent, array &$types, array &$descendants): void |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get abstract types |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | public function abstractTypes(): array |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Clear schema cache |
||
| 518 | * |
||
| 519 | * @return void |
||
| 520 | * @codeCoverageIgnore |
||
| 521 | */ |
||
| 522 | public function clearCache(): void |
||
| 525 | } |
||
| 526 | } |
||
| 527 |