| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 283 |
| Code Lines | 171 |
| Lines | 16 |
| Ratio | 5.65 % |
| 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 |
||
| 177 | public function getModelSchema( |
||
| 178 | $modelName, |
||
| 179 | DocumentModel $model, |
||
| 180 | $online = true, |
||
| 181 | $internal = false, |
||
| 182 | $serialized = false |
||
| 183 | ) { |
||
| 184 | |||
| 185 | $cacheKey = sprintf( |
||
| 186 | 'schema.%s.%s.%s.%s', |
||
| 187 | $model->getEntityClass(), |
||
| 188 | (string) $online, |
||
| 189 | (string) $internal, |
||
| 190 | (string) $serialized |
||
| 191 | ); |
||
| 192 | |||
| 193 | if ($this->cache->contains($cacheKey)) { |
||
| 194 | return $this->cache->fetch($cacheKey); |
||
| 195 | } |
||
| 196 | |||
| 197 | $invalidateCacheMap = []; |
||
| 198 | if ($this->cache->contains($this->cacheInvalidationMapKey)) { |
||
| 199 | $invalidateCacheMap = $this->cache->fetch($this->cacheInvalidationMapKey); |
||
| 200 | } |
||
| 201 | |||
| 202 | // build up schema data |
||
| 203 | $schema = new Schema; |
||
| 204 | |||
| 205 | if (!empty($model->getTitle())) { |
||
| 206 | $schema->setTitle($model->getTitle()); |
||
| 207 | } else { |
||
| 208 | if (!is_null($modelName)) { |
||
| 209 | $schema->setTitle(ucfirst($modelName)); |
||
| 210 | } else { |
||
| 211 | $reflection = new \ReflectionClass($model); |
||
| 212 | $schema->setTitle(ucfirst($reflection->getShortName())); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $schema->setDescription($model->getDescription()); |
||
| 217 | $schema->setDocumentClass($model->getDocumentClass()); |
||
|
|
|||
| 218 | $schema->setRecordOriginModifiable($model->getRecordOriginModifiable()); |
||
| 219 | $schema->setIsVersioning($model->isVersioning()); |
||
| 220 | $schema->setType('object'); |
||
| 221 | |||
| 222 | // grab schema info from model |
||
| 223 | $repo = $model->getRepository(); |
||
| 224 | $meta = $repo->getClassMetadata(); |
||
| 225 | |||
| 226 | // Init sub searchable fields |
||
| 227 | $subSearchableFields = []; |
||
| 228 | |||
| 229 | // look for translatables in document class |
||
| 230 | $documentReflection = new \ReflectionClass($repo->getClassName()); |
||
| 231 | $documentClass = $documentReflection->newInstance(); |
||
| 232 | if ($documentReflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) { |
||
| 233 | /** @var TranslatableDocumentInterface $documentInstance */ |
||
| 234 | $documentInstance = $documentReflection->newInstanceWithoutConstructor(); |
||
| 235 | $translatableFields = array_merge( |
||
| 236 | $documentInstance->getTranslatableFields(), |
||
| 237 | $documentInstance->getPreTranslatedFields() |
||
| 238 | ); |
||
| 239 | } else { |
||
| 240 | $translatableFields = []; |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!empty($translatableFields)) { |
||
| 244 | $invalidateCacheMap[$this->languageRepository->getClassName()][] = $cacheKey; |
||
| 245 | } |
||
| 246 | |||
| 247 | // exposed fields |
||
| 248 | $documentFieldNames = isset($this->documentFieldNames[$repo->getClassName()]) ? |
||
| 249 | $this->documentFieldNames[$repo->getClassName()] : |
||
| 250 | []; |
||
| 251 | |||
| 252 | $languages = []; |
||
| 253 | if ($online) { |
||
| 254 | $languages = array_map( |
||
| 255 | function (Language $language) { |
||
| 256 | return $language->getId(); |
||
| 257 | }, |
||
| 258 | $this->languageRepository->findAll() |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | if (empty($languages)) { |
||
| 262 | $languages = [ |
||
| 263 | $this->defaultLocale |
||
| 264 | ]; |
||
| 265 | } |
||
| 266 | |||
| 267 | // exposed events.. |
||
| 268 | $classShortName = $documentReflection->getShortName(); |
||
| 269 | if (isset($this->eventMap[$classShortName])) { |
||
| 270 | $schema->setEventNames(array_unique($this->eventMap[$classShortName]['events'])); |
||
| 271 | } |
||
| 272 | |||
| 273 | // don't describe hidden fields |
||
| 274 | $requiredFields = $model->getRequiredFields(); |
||
| 275 | if (empty($requiredFields) || !is_array($requiredFields)) { |
||
| 276 | $requiredFields = []; |
||
| 277 | } |
||
| 278 | $requiredFields = array_intersect_key($documentFieldNames, array_combine($requiredFields, $requiredFields)); |
||
| 279 | |||
| 280 | foreach ($meta->getFieldNames() as $field) { |
||
| 281 | // don't describe hidden fields |
||
| 282 | if (!isset($documentFieldNames[$field])) { |
||
| 283 | continue; |
||
| 284 | } |
||
| 285 | |||
| 286 | $isEmptyExtref = false; |
||
| 287 | if (is_callable([$documentClass, 'isEmptyExtRefObject'])) { |
||
| 288 | $isEmptyExtref = $documentClass->isEmptyExtRefObject(); |
||
| 289 | } |
||
| 290 | |||
| 291 | // hide realId field (I was aiming at a cleaner solution than the matching realId string initially) |
||
| 292 | // hide embedded ID field unless it's required or for internal validation need, back-compatibility. |
||
| 293 | // TODO remove !$internal once no clients use it for embedded objects as these id are done automatically |
||
| 294 | if (($meta->getTypeOfField($field) == 'id' && $field == 'realId') || |
||
| 295 | ( |
||
| 296 | $field == 'id' && |
||
| 297 | !$internal && $meta->isEmbeddedDocument && !in_array('id', $requiredFields) && $isEmptyExtref |
||
| 298 | ) |
||
| 299 | ) { |
||
| 300 | continue; |
||
| 301 | } |
||
| 302 | |||
| 303 | $property = new Schema(); |
||
| 304 | $property->setTitle($model->getTitleOfField($field)); |
||
| 305 | $property->setDescription($model->getDescriptionOfField($field)); |
||
| 306 | $property->setType($meta->getTypeOfField($field)); |
||
| 307 | $property->setGroups($model->getGroupsOfField($field)); |
||
| 308 | $property->setReadOnly($model->getReadOnlyOfField($field)); |
||
| 309 | |||
| 310 | // we only want to render if it's true |
||
| 311 | if ($model->getRecordOriginExceptionOfField($field) === true) { |
||
| 312 | $property->setRecordOriginException(true); |
||
| 313 | } |
||
| 314 | |||
| 315 | if ($meta->getTypeOfField($field) === 'many') { |
||
| 316 | $propertyModel = $model->manyPropertyModelForTarget($meta->getAssociationTargetClass($field)); |
||
| 317 | |||
| 318 | if ($model->hasDynamicKey($field)) { |
||
| 319 | $property->setType('object'); |
||
| 320 | |||
| 321 | if ($online) { |
||
| 322 | // we generate a complete list of possible keys when we have access to mongodb |
||
| 323 | // this makes everything work with most json-schema v3 implementations (ie. schemaform.io) |
||
| 324 | $dynamicKeySpec = $model->getDynamicKeySpec($field); |
||
| 325 | |||
| 326 | $documentId = $dynamicKeySpec->{'document-id'}; |
||
| 327 | $dynamicRepository = $this->repositoryFactory->get($documentId); |
||
| 328 | |||
| 329 | // put this in invalidate map so when know we have to invalidate when this document is used |
||
| 330 | $invalidateCacheMap[$dynamicRepository->getDocumentName()][] = $cacheKey; |
||
| 331 | |||
| 332 | $repositoryMethod = $dynamicKeySpec->{'repository-method'}; |
||
| 333 | $records = $dynamicRepository->$repositoryMethod(); |
||
| 334 | |||
| 335 | $dynamicProperties = array_map( |
||
| 336 | function ($record) { |
||
| 337 | return $record->getId(); |
||
| 338 | }, |
||
| 339 | $records |
||
| 340 | ); |
||
| 341 | foreach ($dynamicProperties as $propertyName) { |
||
| 342 | $property->addProperty( |
||
| 343 | $propertyName, |
||
| 344 | $this->getModelSchema($field, $propertyModel, $online, $internal) |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | } else { |
||
| 348 | // swagger case |
||
| 349 | $property->setAdditionalProperties( |
||
| 350 | new SchemaAdditionalProperties( |
||
| 351 | $this->getModelSchema($field, $propertyModel, $online, $internal) |
||
| 352 | ) |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | } else { |
||
| 356 | $property->setItems($this->getModelSchema($field, $propertyModel, $online, $internal)); |
||
| 357 | $property->setType('array'); |
||
| 358 | } |
||
| 359 | } elseif ($meta->getTypeOfField($field) === 'one') { |
||
| 360 | $propertyModel = $model->manyPropertyModelForTarget($meta->getAssociationTargetClass($field)); |
||
| 361 | $property = $this->getModelSchema($field, $propertyModel, $online, $internal); |
||
| 362 | |||
| 363 | if ($property->getSearchable()) { |
||
| 364 | foreach ($property->getSearchable() as $searchableSubField) { |
||
| 365 | $subSearchableFields[] = $field . '.' . $searchableSubField; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } elseif (in_array($field, $translatableFields, true)) { |
||
| 369 | $property = $this->makeTranslatable($property, $languages); |
||
| 370 | } elseif (in_array($field.'[]', $translatableFields, true)) { |
||
| 371 | $property = $this->makeArrayTranslatable($property, $languages); |
||
| 372 | } elseif ($meta->getTypeOfField($field) === 'extref') { |
||
| 373 | $urls = []; |
||
| 374 | $refCollections = $model->getRefCollectionOfField($field); |
||
| 375 | foreach ($refCollections as $collection) { |
||
| 376 | if (isset($this->extrefServiceMapping[$collection])) { |
||
| 377 | $urls[] = $this->router->generate( |
||
| 378 | $this->extrefServiceMapping[$collection].'.all', |
||
| 379 | [], |
||
| 380 | UrlGeneratorInterface::ABSOLUTE_URL |
||
| 381 | ); |
||
| 382 | } elseif ($collection === '*') { |
||
| 383 | $urls[] = '*'; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | $property->setRefCollection($urls); |
||
| 387 | View Code Duplication | } elseif ($meta->getTypeOfField($field) === 'collection') { |
|
| 388 | $itemSchema = new Schema(); |
||
| 389 | $property->setType('array'); |
||
| 390 | $itemSchema->setType($this->getCollectionItemType($meta->name, $field)); |
||
| 391 | |||
| 392 | $property->setItems($itemSchema); |
||
| 393 | $property->setFormat(null); |
||
| 394 | } elseif ($meta->getTypeOfField($field) === 'datearray') { |
||
| 395 | $itemSchema = new Schema(); |
||
| 396 | $property->setType('array'); |
||
| 397 | $itemSchema->setType('string'); |
||
| 398 | $itemSchema->setFormat('date-time'); |
||
| 399 | |||
| 400 | $property->setItems($itemSchema); |
||
| 401 | $property->setFormat(null); |
||
| 402 | View Code Duplication | } elseif ($meta->getTypeOfField($field) === 'hasharray') { |
|
| 403 | $itemSchema = new Schema(); |
||
| 404 | $itemSchema->setType('object'); |
||
| 405 | |||
| 406 | $property->setType('array'); |
||
| 407 | $property->setItems($itemSchema); |
||
| 408 | $property->setFormat(null); |
||
| 409 | } |
||
| 410 | |||
| 411 | if (in_array($meta->getTypeOfField($field), $property->getMinLengthTypes())) { |
||
| 412 | // make sure a required field cannot be blank |
||
| 413 | if (in_array($documentFieldNames[$field], $requiredFields)) { |
||
| 414 | $property->setMinLength(1); |
||
| 415 | } else { |
||
| 416 | // in the other case, make sure also null can be sent.. |
||
| 417 | $currentType = $property->getType(); |
||
| 418 | if ($currentType instanceof SchemaType) { |
||
| 419 | $property->setType(array_merge($currentType->getTypes(), ['null'])); |
||
| 420 | } else { |
||
| 421 | $property->setType('null'); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | $property = $this->constraintBuilder->addConstraints($field, $property, $model); |
||
| 427 | |||
| 428 | $schema->addProperty($documentFieldNames[$field], $property); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * if we generate schema for internal use; don't have id in required array as |
||
| 433 | * it's 'requiredness' depends on the method used (POST/PUT/PATCH) and is checked in checks |
||
| 434 | * before validation. |
||
| 435 | */ |
||
| 436 | $idPosition = array_search('id', $requiredFields); |
||
| 437 | if ($internal === true && $idPosition !== false && !$meta->isEmbeddedDocument) { |
||
| 438 | unset($requiredFields[$idPosition]); |
||
| 439 | } |
||
| 440 | |||
| 441 | $schema->setRequired($requiredFields); |
||
| 442 | |||
| 443 | // set additionalProperties to false (as this is our default policy) if not already set |
||
| 444 | if (is_null($schema->getAdditionalProperties()) && $online) { |
||
| 445 | $schema->setAdditionalProperties(new SchemaAdditionalProperties(false)); |
||
| 446 | } |
||
| 447 | |||
| 448 | $searchableFields = array_merge($subSearchableFields, $model->getSearchableFields()); |
||
| 449 | $schema->setSearchable($searchableFields); |
||
| 450 | |||
| 451 | if ($serialized === true) { |
||
| 452 | $schema = json_decode($this->serializer->serialize($schema, 'json')); |
||
| 453 | } |
||
| 454 | |||
| 455 | $this->cache->save($cacheKey, $schema); |
||
| 456 | $this->cache->save($this->cacheInvalidationMapKey, $invalidateCacheMap); |
||
| 457 | |||
| 458 | return $schema; |
||
| 459 | } |
||
| 460 | |||
| 547 |