| Conditions | 16 |
| Paths | 369 |
| Total Lines | 80 |
| Code Lines | 42 |
| 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 |
||
| 116 | private function getIdentifiersFromItem($item): array |
||
| 117 | { |
||
| 118 | $identifiers = []; |
||
| 119 | $resourceClass = $this->getObjectClass($item); |
||
| 120 | |||
| 121 | $cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass])); |
||
| 122 | |||
| 123 | try { |
||
| 124 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
||
| 125 | |||
| 126 | if ($cacheItem->isHit()) { |
||
| 127 | foreach ($cacheItem->get() as $propertyName) { |
||
| 128 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
||
| 129 | |||
| 130 | if (is_object($identifiers[$propertyName])) { |
||
| 131 | $relatedCacheKey = self::CACHE_KEY_PREFIX.md5(serialize($this->getObjectClass($identifiers[$propertyName]))); |
||
| 132 | |||
| 133 | $cacheItem = $this->cacheItemPool->getItem($relatedCacheKey); |
||
| 134 | |||
| 135 | if (!$cacheItem->isHit()) { |
||
| 136 | throw new CacheException(); |
||
| 137 | } |
||
| 138 | |||
| 139 | $identifiers[$propertyName] = $cacheItem->get()[0]; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $identifiers; |
||
| 144 | } |
||
| 145 | } catch (CacheException $e) { |
||
| 146 | // do nothing |
||
| 147 | } |
||
| 148 | |||
| 149 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
||
| 150 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
| 151 | |||
| 152 | $identifier = $propertyMetadata->isIdentifier(); |
||
| 153 | if (null === $identifier || false === $identifier) { |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
||
| 158 | |||
| 159 | if (!is_object($identifiers[$propertyName])) { |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | |||
| 163 | $relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]); |
||
| 164 | $relatedItem = $identifiers[$propertyName]; |
||
| 165 | |||
| 166 | unset($identifiers[$propertyName]); |
||
| 167 | |||
| 168 | foreach ($this->propertyNameCollectionFactory->create($relatedResourceClass) as $relatedPropertyName) { |
||
| 169 | $propertyMetadata = $this->propertyMetadataFactory->create($relatedResourceClass, $relatedPropertyName); |
||
| 170 | |||
| 171 | if ($propertyMetadata->isIdentifier()) { |
||
| 172 | if (isset($identifiers[$propertyName])) { |
||
| 173 | throw new RuntimeException(sprintf('Composite identifiers not supported in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
||
| 174 | } |
||
| 175 | |||
| 176 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if (!isset($identifiers[$propertyName])) { |
||
| 181 | throw new RuntimeException(sprintf('No identifier found in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | if (isset($cacheItem)) { |
||
| 186 | try { |
||
| 187 | $cacheItem->set(array_keys($identifiers)); |
||
| 188 | $this->cacheItemPool->save($cacheItem); |
||
| 189 | } catch (CacheException $e) { |
||
| 190 | // do nothing |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $identifiers; |
||
| 195 | } |
||
| 196 | |||
| 217 |