Test Failed
Push — fix/media-validation ( 3351fe...390e9c )
by Ben
25:53
created

ImageFieldHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 1
b 0
f 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detach() 0 5 1
A new() 0 19 3
A handle() 0 10 3
A replace() 0 9 1
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Media\Application;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\AssetLibrary\Asset;
7
use Thinktomorrow\AssetLibrary\HasAsset;
8
use Thinktomorrow\Chief\Fields\Types\MediaField;
9
use Thinktomorrow\Chief\Media\DuplicateAssetException;
10
11
class ImageFieldHandler extends AbstractMediaFieldHandler
12
{
13
    public function handle(HasAsset $model, MediaField $field, Request $request): void
14
    {
15
        // Parse request ...
16
        $mediaRequest = $this->mediaRequest([
17
            $request->input('images.'.$field->getName(), []),
18
        ], $field, $request);
19
20
        foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
21
            foreach($mediaRequest->getByKey($action) as $input) {
22
                $this->$action($model, $input);
23
            }
24
        }
25
26
        // TODO: sort the assets as well... perhaps can this be done in the mediaRequest class???
27
    }
28
29
//    private function mediaRequest(MediaField $field, Request $request): MediaRequest
30
//    {
31
//        $mediaRequest = new MediaRequest();
32
//
33
//        foreach($request->input('images.'.$field->getName(), []) as $locale => $filesPerLocale) {
34
//
35
//            foreach($filesPerLocale as $action => $files) {
36
//                foreach($files as $k => $file) {
37
//
38
//                    // If the passed value is null, we do not want to process it.
39
//                    if(!$file) continue;
40
//
41
//                    $mediaRequest->add($action, new MediaRequestInput(
42
//                        $file, $locale, $field->getKey(), [
43
//                            'index' => $k,
44
//                            'existing_asset' => $this->refersToExistingAsset($file),
45
//                        ] // index key is used for e.g. replace method to indicate the current asset id
46
//                    ));
47
//                }
48
//            }
49
//
50
//        }
51
//
52
//        return $mediaRequest;
53
//    }
54
55
    private function new(HasAsset $model, MediaRequestInput $mediaRequestInput): Asset
56
    {
57
        if($mediaRequestInput->metadata('existing_asset')) {
58
59
            $existingAsset = Asset::find($mediaRequestInput->value());
60
61
            if ($model->assetRelation()->where('asset_pivots.type', $mediaRequestInput->type())->where('asset_pivots.locale', $mediaRequestInput->locale())->get()->contains($existingAsset)) {
62
                throw new DuplicateAssetException();
63
            }
64
65
            return $this->addAsset->add($model, $existingAsset, $mediaRequestInput->type(), $mediaRequestInput->locale());
66
        }
67
68
        // Inputted value is expected to be a slim specific json string.
69
        $base64FileString = json_decode($mediaRequestInput->value())->output->image;
70
71
        $filename = json_decode($mediaRequestInput->value())->output->name;
72
73
        return $this->addAsset->add($model, $base64FileString, $mediaRequestInput->type(), $mediaRequestInput->locale(), $this->sluggifyFilename($filename));
74
    }
75
76
    private function replace(HasAsset $model, MediaRequestInput $mediaRequest): Asset
77
    {
78
        $asset = $this->new($model, $mediaRequest);
79
80
        $currentAssetId = $mediaRequest->metadata('index');
81
82
        $this->replaceAsset->handle($model, $currentAssetId, $asset->id);
0 ignored issues
show
Deprecated Code introduced by
The function Thinktomorrow\AssetLibra...\ReplaceAsset::handle() has been deprecated: leaving the type and locale empty is deprecated and is no longer supported from 0.7.0 ( Ignorable by Annotation )

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

82
        /** @scrutinizer ignore-deprecated */ $this->replaceAsset->handle($model, $currentAssetId, $asset->id);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83
84
        return $asset;
85
    }
86
87
    private function detach(HasAsset $model, MediaRequestInput $mediaRequest)
88
    {
89
        $assetId = $mediaRequest->value();
90
91
        $this->detachAsset->detach($model, $assetId, $mediaRequest->type(), $mediaRequest->locale());
92
    }
93
}
94