| Conditions | 6 |
| Paths | 6 |
| Total Lines | 53 |
| Code Lines | 18 |
| 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 declare(strict_types=1); |
||
| 45 | protected function handlePayload(HasAsset $model, MediaField $field, string $locale, $values) |
||
| 46 | { |
||
| 47 | foreach ($values as $key => $value) { |
||
| 48 | |||
| 49 | $keyIsAttachedAssetId = $this->isKeyAnAttachedAssetId($model->assetRelation, $locale, $field->getKey(), $key); |
||
|
|
|||
| 50 | |||
| 51 | if ($this->shouldNotBeProcessed($value, $key, $keyIsAttachedAssetId)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | |||
| 55 | /* |
||
| 56 | * when value is null, it means that the asset is queued for detachment |
||
| 57 | * is key isn't an attached asset reference, we ignore it because this |
||
| 58 | * means that a newly uploaded asses is deleted in the same request |
||
| 59 | */ |
||
| 60 | if (is_null($value)) { |
||
| 61 | |||
| 62 | if ($keyIsAttachedAssetId) { |
||
| 63 | $this->detach($model, $locale, $field->getKey(), $key); |
||
| 64 | } |
||
| 65 | |||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | // If key refers to an already existing asset, it is queued for replacement by a new one |
||
| 70 | if ($keyIsAttachedAssetId) { |
||
| 71 | $this->replace($model, $locale, $field->getKey(), $key, $value); |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->new($model, $locale, $field->getKey(), $value); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | abstract protected function new(HasAsset $model, string $locale, string $type, $value): Asset; |
||
| 80 | |||
| 81 | protected function replace(HasAsset $model, string $locale, string $type, $currentAssetId, $value): Asset |
||
| 82 | { |
||
| 83 | $asset = $this->looksLikeAnAssetId($value) |
||
| 84 | ? Asset::find($value) |
||
| 85 | : $this->createNewAsset($model, $locale, $type, $value); |
||
| 86 | |||
| 87 | $this->replaceAsset->handle($model, $currentAssetId, $asset->id, $type, $locale); |
||
| 88 | |||
| 89 | return $asset; |
||
| 90 | } |
||
| 91 | |||
| 92 | protected function detach(HasAsset $model, string $locale, string $type, $assetId) |
||
| 93 | { |
||
| 94 | $this->detachAsset->detach($model, $assetId, $type, $locale); |
||
| 95 | } |
||
| 96 | |||
| 97 | abstract protected function createNewAsset(HasAsset $model, string $locale, string $type, $value): Asset; |
||
| 98 | |||
| 184 |