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