Test Failed
Push — fix/media-validation ( 6403fc...e4808d )
by Ben
07:03
created

FileFieldHandler::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 declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Media\Application;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Thinktomorrow\AssetLibrary\Asset;
8
use Thinktomorrow\AssetLibrary\HasAsset;
9
use Thinktomorrow\Chief\Fields\Types\MediaField;
10
use Thinktomorrow\Chief\Media\DuplicateAssetException;
11
12
class FileFieldHandler extends AbstractMediaFieldHandler
13
{
14
    public function handle(HasAsset $model, MediaField $field, Request $request): void
15
    {
16
        // Parse request ...
17
        $mediaRequest = $this->mediaRequest([
18
            $request->file('files.'.$field->getName(), []),
19
            $request->input('files.'.$field->getName(), []),
20
        ], $field, $request);
21
22
        foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
23
            foreach($mediaRequest->getByKey($action) as $input) {
24
                $this->$action($model, $input);
25
            }
26
        }
27
28
        // TODO: sort the assets as well... perhaps can this be done in the mediaRequest class???
29
30
    }
31
32
    private function new(HasAsset $model, MediaRequestInput $mediaRequestInput): Asset
33
    {
34
        if($mediaRequestInput->metadata('existing_asset')) {
35
            return $this->newExistingAsset($model, $mediaRequestInput);
36
        }
37
38
        /** @var UploadedFile $uploadedFile */
39
        $uploadedFile = $mediaRequestInput->value();
40
41
        $filename = $uploadedFile->getClientOriginalName();
42
43
        return $this->addAsset->add($model, $uploadedFile, $mediaRequestInput->type(), $mediaRequestInput->locale(), $this->sluggifyFilename($filename));
44
    }
45
46
    private function replace(HasAsset $model, MediaRequestInput $mediaRequest): Asset
47
    {
48
        $asset = $this->createNewAsset($model, $mediaRequest);
49
50
        $currentAssetId = $mediaRequest->metadata('index');
51
52
        $this->replaceAsset->handle($model, $currentAssetId, $asset->id, $mediaRequest->type(), $mediaRequest->locale());
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

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

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...
53
54
        return $asset;
55
    }
56
57
    private function detach(HasAsset $model, MediaRequestInput $mediaRequest)
58
    {
59
        $assetId = $mediaRequest->value();
60
61
        $this->detachAsset->detach($model, $assetId, $mediaRequest->type(), $mediaRequest->locale());
62
    }
63
64
    protected function createNewAsset(HasAsset $model, MediaRequestInput $mediaRequestInput): Asset
65
    {
66
        if($mediaRequestInput->metadata('existing_asset')) {
67
            return Asset::find($mediaRequestInput->value());
0 ignored issues
show
Bug Best Practice introduced by
The expression return Thinktomorrow\Ass...aRequestInput->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...
68
        }
69
70
        /** @var UploadedFile $uploadedFile */
71
        $uploadedFile = $mediaRequestInput->value();
72
73
        $filename = $uploadedFile->getClientOriginalName();
74
75
        return $this->assetUploader->upload($uploadedFile, $filename);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->assetUploa...ploadedFile, $filename) could return the type Illuminate\Support\Collection which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Asset. Consider adding an additional type-check to rule them out.
Loading history...
76
    }
77
}
78