Test Failed
Push — fix/media-validation ( 3351fe )
by Ben
09:34
created

FileFieldHandler::mediaRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 4
nc 4
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
11
class FileFieldHandler extends AbstractMediaFieldHandler
12
{
13
    public function handle(HasAsset $model, MediaField $field, Request $request): void
14
    {
15
        // Parse request ...
16
        $mediaRequest = $this->mediaRequest($field, $request);
17
trap($mediaRequest);
18
        foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
19
            foreach($mediaRequest->getByKey($action) as $input) {
20
                $this->$action($model, $input);
21
            }
22
        }
23
24
        // TODO: sort the assets as well... perhaps can this be done in the mediaRequest class???
25
26
    }
27
28
    private function mediaRequest(MediaField $field, Request $request): MediaRequest
29
    {
30
        $mediaRequest = new MediaRequest();
31
32
        foreach($request->file('files.'.$field->getName(), []) as $locale => $filesPerLocale) {
33
34
            foreach($filesPerLocale as $action => $files) {
35
                foreach($files as $k => $file) {
36
                    $mediaRequest->add($action, new MediaRequestInput(
37
                        $file, $locale, $field->getKey(), ['index' => $k] // index key is used for replace method to indicate the current asset id
38
                    ));
39
                }
40
            }
41
42
        }
43
44
        return $mediaRequest;
45
    }
46
47
    private function new(HasAsset $model, MediaRequestInput $mediaRequest): Asset
48
    {
49
        /** @var UploadedFile $uploadedFile */
50
        $uploadedFile = $mediaRequest->value();
51
52
        $filename = $uploadedFile->getClientOriginalName();
53
54
        return $this->addAsset->add($model, $uploadedFile, $mediaRequest->type(), $mediaRequest->locale(), $this->sluggifyFilename($filename));
55
    }
56
57
    private function replace(HasAsset $model, MediaRequestInput $mediaRequest): Asset
58
    {
59
        $asset = $this->add($model, $mediaRequest);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Thinktomorrow\Chief\Medi...cation\FileFieldHandler. ( Ignorable by Annotation )

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

59
        /** @scrutinizer ignore-call */ 
60
        $asset = $this->add($model, $mediaRequest);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        $currentAssetId = $mediaRequest->metadata('index');
62
63
        $this->replaceAsset->handle($model, $currentAssetId, $asset->id);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Asset. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
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

63
        /** @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...
64
    }
65
66
    private function detach(HasAsset $model, MediaRequestInput $mediaRequest)
67
    {
68
        $assetId = $mediaRequest->value();
69
70
        $this->detachAsset->detach($model, $assetId, $mediaRequest->type(), $mediaRequest->locale());
71
    }
72
}
73