Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 54
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 48 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint\Image;
13
14
use InvalidArgumentException;
15
use Tubee\Storage\Validator as StorageValidator;
16
17
class Validator
18
{
19
    /**
20
     * Validate resource.
21
     */
22
    public static function validate(array $resource): array
23
    {
24
        $defaults = [
25
            'storage' => [
26
                'kind' => 'StreamStorage',
27
            ],
28
            'resource' => [
29
                'format' => null,
30
                'min_width' => null,
31
                'max_width' => null,
32
            ],
33
        ];
34
35
        $resource = array_replace_recursive($defaults, $resource);
36
37
        if (!isset($resource['file']) || !is_string($resource['file'])) {
38
            throw new InvalidArgumentException('file is required and must be a string');
39
        }
40
41
        foreach ($resource['resource'] as $key => $value) {
42
            if (is_null($value)) {
43
                continue;
44
            }
45
46
            switch ($key) {
47
                case 'format':
48
                    if (!is_string($value)) {
49
                        throw new InvalidArgumentException("resource.$key must be a string");
50
                    }
51
52
                break;
53
                case 'min_width':
54
                case 'max_width':
55
                    if (!is_int($value)) {
56
                        throw new InvalidArgumentException("resource.$key must be an integer");
57
                    }
58
59
                    break;
60
                default:
61
                    throw new InvalidArgumentException('unknown option resource.'.$key.' provided');
62
            }
63
        }
64
65
        $resource = array_replace_recursive($defaults, $resource);
66
        $resource['storage'] = StorageValidator::validate($resource['storage']);
67
68
        return $resource;
69
    }
70
}
71