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

FileFieldMinRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateMin() 0 7 2
A validate() 0 24 6
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Fields\ValidationRules;
4
5
use Symfony\Component\HttpFoundation\File\File;
6
use Thinktomorrow\Chief\Media\Application\MediaRequest;
7
8
class FileFieldMinRule extends AbstractMediaFieldRule
9
{
10
    public function validate($attribute, $value, $params, $validator): bool
11
    {
12
        $value = $this->normalizePayload($value);
13
14
        foreach([MediaRequest::NEW, MediaRequest::REPLACE] as $type) {
15
            foreach($value[$type] as $file) {
16
                if($file && false !== $this->validateMin($attribute, $file, $params)) {
17
                    return true;
18
                }
19
            }
20
        }
21
22
        $validator->setCustomMessages([
23
            'filefield_min' => 'De :attribute is te klein en dient groter te zijn dan ' . implode(',',$params) .'Kb.',
24
        ]);
25
26
        if(!isset($validator->customAttributes[$attribute])) {
27
            $validator->addCustomAttributes([
28
                $attribute => 'afbeelding',
29
            ]);
30
        }
31
32
33
        return false;
34
    }
35
36
    public function validateMin($attribute, $value, $parameters)
37
    {
38
        if($this->refersToExistingAsset($value)) {
39
            return $this->validateAssetMin($this->existingAsset($value), $parameters);
40
        }
41
42
        return parent::validateMin($attribute, $value, $parameters);
43
    }
44
}
45