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

AbstractMediaFieldHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mediaRequest() 0 20 5
A refersToExistingAsset() 0 3 1
A sluggifyFilename() 0 8 2
A __construct() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Media\Application;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Thinktomorrow\Chief\Fields\Types\MediaField;
8
use Thinktomorrow\AssetLibrary\Application\AddAsset;
9
use Thinktomorrow\AssetLibrary\Application\DetachAsset;
10
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset;
11
12
abstract class AbstractMediaFieldHandler
13
{
14
    /** @var ReplaceAsset */
15
    protected $replaceAsset;
16
17
    /** @var AddAsset */
18
    protected $addAsset;
19
20
    /** @var DetachAsset */
21
    protected $detachAsset;
22
23
    final public function __construct(AddAsset $addAsset, ReplaceAsset $replaceAsset, DetachAsset $detachAsset)
24
    {
25
        $this->replaceAsset = $replaceAsset;
26
        $this->addAsset = $addAsset;
27
        $this->detachAsset = $detachAsset;
28
    }
29
30
    protected function mediaRequest(array $requests, MediaField $field, Request $request): MediaRequest
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

30
    protected function mediaRequest(array $requests, MediaField $field, /** @scrutinizer ignore-unused */ Request $request): MediaRequest

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $mediaRequest = new MediaRequest();
33
34
        foreach($requests as $requestData){
35
            foreach($requestData as $locale => $filesPerLocale) {
36
                foreach($filesPerLocale as $action => $files) {
37
                    foreach($files as $k => $file) {
38
                        $mediaRequest->add($action, new MediaRequestInput(
39
                            $file, $locale, $field->getKey(), [
40
                                'index' => $k, // index key is used for replace method to indicate the current asset id
41
                                'existing_asset' => $this->refersToExistingAsset($file),
42
                            ]
43
                        ));
44
                    }
45
                }
46
            }
47
        }
48
49
        return $mediaRequest;
50
    }
51
52
    protected function refersToExistingAsset($value): bool
53
    {
54
        return is_int($value);
55
    }
56
57
    /**
58
     * @param string $filename
59
     * @return string
60
     */
61
    protected function sluggifyFilename(string $filename): string
62
    {
63
        if(false === strpos($filename, '.')) return $filename;
64
65
        $extension = substr($filename, strrpos($filename, '.') + 1);
66
        $filename = substr($filename, 0, strrpos($filename, '.'));
67
68
        return Str::slug($filename) . '.' . $extension;
69
    }
70
}
71