Test Failed
Push — fix/slim ( 4806d2...e1cc5e )
by Ben
06:18
created

ImageFieldHandler::createNewAsset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Media\Application;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Thinktomorrow\AssetLibrary\Asset;
10
use Thinktomorrow\AssetLibrary\HasAsset;
11
use Thinktomorrow\Chief\Fields\Types\MediaField;
12
13
class ImageFieldHandler extends AbstractMediaFieldHandler
14
{
15
    public function handle(HasAsset $model, MediaField $field, Request $request): void
16
    {
17
        $existingAttactedAssets = $model->assetRelation;
0 ignored issues
show
Bug introduced by
Accessing assetRelation on the interface Thinktomorrow\AssetLibrary\HasAsset suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
18
19
        foreach($request->input('images.' . $field->getName(), []) as $locale => $values) {
20
            foreach ($values as $key => $value) {
21
22
                // value is null ?
23
                if (is_null($value)) {
24
                    $this->detach($model, $value);
0 ignored issues
show
Bug introduced by
$value of type null is incompatible with the type Thinktomorrow\Chief\Medi...ation\MediaRequestInput expected by parameter $mediaRequest of Thinktomorrow\Chief\Medi...eFieldHandler::detach(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
                    $this->detach($model, /** @scrutinizer ignore-type */ $value);
Loading history...
25
                    continue;
26
                }
27
28
                // Does key refer to existing asset? -> replace
29
                $keyRefersToAttachedAsset = $existingAttactedAssets
30
                    ->where('pivot.locale', $locale)
31
                    ->where('pivot.type', $field->getKey())
32
                    ->where('pivot.entity_id', $key)
33
                    ->count();
34
35
                if($keyRefersToAttachedAsset) {
36
                    $this->replace($model, new MediaRequestInput($value, $locale, $field->getKey()));
0 ignored issues
show
Bug introduced by
new Thinktomorrow\Chief\...cale, $field->getKey()) of type Thinktomorrow\Chief\Medi...ation\MediaRequestInput is incompatible with the type string expected by parameter $locale of Thinktomorrow\Chief\Medi...FieldHandler::replace(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
                    $this->replace($model, /** @scrutinizer ignore-type */ new MediaRequestInput($value, $locale, $field->getKey()));
Loading history...
Bug introduced by
The call to Thinktomorrow\Chief\Medi...FieldHandler::replace() has too few arguments starting with type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
                    $this->/** @scrutinizer ignore-call */ 
37
                           replace($model, new MediaRequestInput($value, $locale, $field->getKey()));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
The call to Thinktomorrow\Chief\Medi...estInput::__construct() has too few arguments starting with metadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
                    $this->replace($model, /** @scrutinizer ignore-call */ new MediaRequestInput($value, $locale, $field->getKey()));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
                }
38
39
                trap($key, $value, $existingAttactedAssets, $keyRefersToAttachedAsset);
40
41
            }
42
43
        }
44
45
        // Parse request ...
46
        $mediaRequest = $this->mediaRequest([
47
            $request->input('images.' . $field->getName(), []),
48
        ], $field, $request);
49
trap($request->all(),$mediaRequest);
50
        foreach ([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
51
            foreach ($mediaRequest->getByKey($action) as $input) {
52
                $this->$action($model, $input);
53
            }
54
        }
55
56
        $this->sort($model, $field, $request);
57
    }
58
59
    private function new(HasAsset $model, MediaRequestInput $mediaRequestInput): Asset
60
    {
61
        if ($mediaRequestInput->metadata('value_as_assetid')) {
62
            return $this->newExistingAsset($model, $mediaRequestInput);
0 ignored issues
show
Bug introduced by
$mediaRequestInput of type Thinktomorrow\Chief\Medi...ation\MediaRequestInput is incompatible with the type string expected by parameter $locale of Thinktomorrow\Chief\Medi...ler::newExistingAsset(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            return $this->newExistingAsset($model, /** @scrutinizer ignore-type */ $mediaRequestInput);
Loading history...
Bug introduced by
The call to Thinktomorrow\Chief\Medi...ler::newExistingAsset() has too few arguments starting with type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            return $this->/** @scrutinizer ignore-call */ newExistingAsset($model, $mediaRequestInput);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        }
64
65
        // Inputted value is expected to be a slim specific json string.
66
        $base64FileString = json_decode($mediaRequestInput->value())->output->image;
67
68
        $filename = json_decode($mediaRequestInput->value())->output->name;
69
70
        return $this->addAsset->add($model, $base64FileString, $mediaRequestInput->type(), $mediaRequestInput->locale(), $this->sluggifyFilename($filename));
71
    }
72
73
    private function replace(HasAsset $model, string $locale, string $type, $value): Asset
74
    {
75
        $asset = $this->looksLikeAnAssetID($value)
76
            ? $this->newExistingAsset($model, $locale, $type, $value)
77
            : $this->createNewAsset($model, $locale, $type, $value);
0 ignored issues
show
Unused Code introduced by
The call to Thinktomorrow\Chief\Medi...ndler::createNewAsset() has too many arguments starting with $type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            : $this->/** @scrutinizer ignore-call */ createNewAsset($model, $locale, $type, $value);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
79
        $currentAssetId = $mediaRequestInput->metadata('existing_id');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mediaRequestInput seems to be never defined.
Loading history...
80
81
        $this->replaceAsset->handle($model, $currentAssetId, $asset->id, $mediaRequestInput->type(), $mediaRequestInput->locale());
82
83
        return $asset;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $asset could return the type null which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Asset. Consider adding an additional type-check to rule them out.
Loading history...
84
    }
85
86
    private function detach(HasAsset $model, MediaRequestInput $mediaRequest)
87
    {
88
        $assetId = $mediaRequest->metadata('existing_id');
89
90
        $this->detachAsset->detach($model, $assetId, $mediaRequest->type(), $mediaRequest->locale());
91
    }
92
93
    protected function createNewAsset(HasAsset $model, $value): Asset
94
    {
95
        if($this->looksLikeAnAssetID($value)) {
96
            return Asset::find($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Thinktomorrow\Ass...ary\Asset::find($value) could return the type null which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Asset. Consider adding an additional type-check to rule them out.
Loading history...
97
        }
98
99
        // Inputted value is expected to be a slim specific json string.
100
        $file = json_decode($value)->output->image;
101
102
        $filename = json_decode($value)->output->name;
103
104
        return $this->assetUploader->uploadFromBase64($file, $filename);
105
    }
106
}
107