| Conditions | 7 |
| Paths | 8 |
| Total Lines | 74 |
| 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 |
||
| 47 | public function hydrate($documentsArray) |
||
| 48 | { |
||
| 49 | if (!is_array($documentsArray)) { |
||
|
|
|||
| 50 | throw new HydratorException('Result dataset must be an array'); |
||
| 51 | } |
||
| 52 | |||
| 53 | if (empty($documentsArray)) { |
||
| 54 | throw new HydratorException('Result dataset is empty'); |
||
| 55 | } |
||
| 56 | |||
| 57 | $scoreStatements = []; |
||
| 58 | $discriminatorField = $this->schema->getDiscriminatorConfigField(); |
||
| 59 | $entityPrimaryKeyField = $this->schema->getEntityPrimaryKeyField(); |
||
| 60 | $primaryKeyValues = []; |
||
| 61 | $orderValue = count($documentsArray); |
||
| 62 | foreach($documentsArray as $documentRow) { |
||
| 63 | if (!array_key_exists($discriminatorField->getDocumentFieldName(), $documentRow)) { |
||
| 64 | throw new DiscriminatorFieldException( |
||
| 65 | sprintf('Discriminator field "%s" must be presented in dataset', $discriminatorField->getDocumentFieldName()) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($discriminatorField->getValue($this->entityConfig) !== $documentRow[$discriminatorField->getDocumentFieldName()]) { |
||
| 70 | throw new DiscriminatorFieldException( |
||
| 71 | sprintf( |
||
| 72 | 'Discriminator field value "%s" must be "%s" but "%s" is provided', |
||
| 73 | $discriminatorField->getConfigFieldName(), |
||
| 74 | $discriminatorField->getValue($this->entityConfig), |
||
| 75 | $documentRow[$discriminatorField->getDocumentFieldName()] |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $primaryKeyValues[] = $documentRow[$entityPrimaryKeyField->getDocumentFieldName()]; |
||
| 81 | $scoreStatements[] = sprintf( |
||
| 82 | 'WHEN entity.%s = %s THEN %s', |
||
| 83 | $entityPrimaryKeyField->getEntityFieldName(), |
||
| 84 | $documentRow[$entityPrimaryKeyField->getDocumentFieldName()], |
||
| 85 | $orderValue |
||
| 86 | ); |
||
| 87 | $orderValue = --$orderValue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $entities = $this->entityRepository |
||
| 91 | ->createQueryBuilder('entity') |
||
| 92 | ->addSelect(sprintf('(CASE %s ELSE 0 END) AS HIDDEN score', join(' ' ,$scoreStatements))) |
||
| 93 | ->where( |
||
| 94 | sprintf('entity.%s in (:ids)', $entityPrimaryKeyField->getEntityFieldName()) |
||
| 95 | ) |
||
| 96 | ->setParameter(':ids', $primaryKeyValues) |
||
| 97 | ->addOrderBy('score', 'desc') |
||
| 98 | ->getQuery()->getResult() |
||
| 99 | ; |
||
| 100 | |||
| 101 | if (count($entities) != count($documentsArray)) { |
||
| 102 | $entityIds = array_map( |
||
| 103 | function($entity) use ($entityPrimaryKeyField) |
||
| 104 | { |
||
| 105 | return $entityPrimaryKeyField->getEntityFieldValue($entity); |
||
| 106 | }, |
||
| 107 | $entities |
||
| 108 | ); |
||
| 109 | |||
| 110 | $notFoundEntityIds = array_diff($primaryKeyValues, $entityIds); |
||
| 111 | throw new HydratorException( |
||
| 112 | sprintf( |
||
| 113 | 'Entities of "%s" with "%s" primary keys are not found in database', |
||
| 114 | $this->entityConfig['class'], |
||
| 115 | join(', ', $notFoundEntityIds) |
||
| 116 | ) |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $entities; |
||
| 121 | } |
||
| 122 | } |