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

validateSlimOutputDimensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Fields\ValidationRules;
4
5
use Thinktomorrow\Chief\Media\Application\MediaRequest;
6
7
class ImageFieldDimensionsRule extends AbstractMediaFieldRule
8
{
9
    public function validate($attribute, $value, $params, $validator): bool
10
    {
11
        $value = $this->normalizePayload($value);
12
13
        foreach ([MediaRequest::NEW, MediaRequest::REPLACE] as $type) {
14
            foreach ($value[$type] as $file) {
15
                if ($file && false !== $this->validateDimensions($attribute, $file, $params)) {
16
                    return true;
17
                }
18
            }
19
        }
20
21
        $validator->setCustomMessages([
22
            'imagefield_dimensions' => 'De :attribute heeft niet de juiste afmetingen: ' . implode(',', $this->humanReadableParams($params)),
23
        ]);
24
25
        if (!isset($validator->customAttributes[$attribute])) {
26
            $validator->addCustomAttributes([
27
                $attribute => 'afbeelding',
28
            ]);
29
        }
30
31
32
        return false;
33
    }
34
35
    public function validateDimensions($attribute, $value, $parameters)
36
    {
37
        if($this->refersToExistingAsset($value)) {
38
            return $this->validateAssetDimensions($this->existingAsset($value), $parameters);
39
        }
40
41
        return $this->validateSlimOutputDimensions($attribute, $value, $parameters);
42
    }
43
44
    /**
45
     * Override Laravel validateDimensions to focus on the ImageField specifics
46
     */
47
    private function validateSlimOutputDimensions($attribute, $value, $parameters)
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

47
    private function validateSlimOutputDimensions(/** @scrutinizer ignore-unused */ $attribute, $value, $parameters)

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...
48
    {
49
        $file = json_decode($value)->output;
50
51
        $width = $file->width;
52
        $height = $file->height;
53
54
        return $this->dimensionsCheck($width, $height, $parameters);
55
    }
56
57
    /**
58
     * @param $params
59
     * @return array
60
     */
61
    private function humanReadableParams($params): array
62
    {
63
        $paramReplacements = [
64
            'min_width'  => 'minimum breedte: %s Kb',
65
            'max_width'  => 'maximum breedte: %s Kb',
66
            'min_height' => 'minimum hoogte: %s Kb',
67
            'max_height' => 'maximum hoogte: %s Kb',
68
            'ratio'      => 'verwachtte verhouding: %s',
69
        ];
70
71
        $humanReadableParams = [];
72
73
        foreach ($params as $param) {
74
75
            list($property, $value) = explode('=', $param);
76
77
            $humanReadableParams[] = isset($paramReplacements[$property])
78
                ? sprintf($paramReplacements[$property], $value)
79
                : $param;
80
        }
81
82
        return $humanReadableParams;
83
    }
84
85
86
}
87