Test Failed
Push — fix/media-validation ( 390e9c...bd2169 )
by Ben
06:53
created

AbstractMediaFieldRule::getSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Fields\ValidationRules;
4
5
use Symfony\Component\HttpFoundation\File\File;
6
use Illuminate\Validation\Concerns\ValidatesAttributes;
7
use Thinktomorrow\Chief\Media\Application\MediaRequest;
8
9
abstract class AbstractMediaFieldRule
10
{
11
    use ValidatesAttributes,
0 ignored issues
show
introduced by
The trait Illuminate\Validation\Concerns\ValidatesAttributes requires some properties which are not provided by Thinktomorrow\Chief\Fiel...\AbstractMediaFieldRule: $data, $container, $currentRule, $implicitAttributes
Loading history...
introduced by
The trait Thinktomorrow\Chief\Fiel...ExistingAssetAttributes requires some properties which are not provided by Thinktomorrow\Chief\Fiel...\AbstractMediaFieldRule: $media, $size
Loading history...
12
        ValidatesExistingAssetAttributes;
13
14
    protected function normalizePayload($value): array
15
    {
16
        $payload = $this->emptyPayload();
17
18
        if(!$value || !is_array($value)) return $payload;
19
20
        foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
21
            if(isset($value[$action])){
22
                $payload[$action] = $value[$action];
23
            }
24
        }
25
26
        return $payload;
27
    }
28
29
    private function emptyPayload(): array
30
    {
31
        return [
32
            MediaRequest::NEW => [],
33
            MediaRequest::REPLACE => [],
34
            MediaRequest::DETACH => [],
35
        ];
36
    }
37
38
    /**
39
     * Default getSize method
40
     *
41
     * Override the default getSize from ValidatesAttributes to avoid calls to a hasRule method
42
     * For media fields this is not needed anyways.
43
     *
44
     * @param $attribute
45
     * @param $value
46
     * @return bool|false|float|int
47
     */
48
    protected function getSize($attribute, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute 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

48
    protected function getSize(/** @scrutinizer ignore-unused */ $attribute, $value)

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...
49
    {
50
        if ($value instanceof File) {
51
            return $value->getSize() / 1024;
52
        }
53
54
        return mb_strlen($value);
55
    }
56
}
57