Completed
Push — master ( abe227...316baf )
by Raffael
13:55 queued 08:01
created

Validator::validate()   D

Complexity

Conditions 19
Paths 15

Size

Total Lines 64

Duplication

Lines 6
Ratio 9.38 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
dl 6
loc 64
ccs 0
cts 54
cp 0
rs 4.5166
c 0
b 0
f 0
cc 19
nc 15
nop 1
crap 380

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
13
14
use InvalidArgumentException;
15
use Tubee\Resource\Validator as ResourceValidator;
16
17
class Validator extends ResourceValidator
18
{
19
    /**
20
     * Validate resource.
21
     */
22
    public static function validate(array $resource): array
23
    {
24
        $resource = parent::validate($resource);
25
26
        $defaults = [
27
            'data' => [
28
                'type' => EndpointInterface::TYPE_BROWSE,
29
                'options' => [
30
                    'identifier' => null,
31
                    'flush' => false,
32
                    'import' => [],
33
                    'filter_one' => null,
34
                    'filter_all' => null,
35
                ],
36
                'resource' => [],
37
            ],
38
        ];
39
40 View Code Duplication
        if (!isset($resource['kind']) || !isset(EndpointInterface::ENDPOINT_MAP[$resource['kind']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            throw new InvalidArgumentException('invalid endpoint kind provided, provide one of ['.join(',', array_flip(EndpointInterface::ENDPOINT_MAP)).']');
42
        }
43
44
        $resource = array_replace_recursive($defaults, $resource);
45 View Code Duplication
        if (!in_array($resource['data']['type'], EndpointInterface::VALID_TYPES)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            throw new InvalidArgumentException('invalid endpoint type provided, provide one of ['.join(',', EndpointInterface::VALID_TYPES).']');
47
        }
48
49
        if ($resource['data']['type'] === EndpointInterface::TYPE_SOURCE && (!is_array($resource['data']['options']['import']) || count($resource['data']['options']['import']) === 0)) {
50
            throw new InvalidArgumentException('source endpoint must include at least one options.import attribute');
51
        }
52
53
        if ($resource['data']['type'] === EndpointInterface::TYPE_DESTINATION && !isset($resource['data']['options']['filter_one'])) {
54
            throw new InvalidArgumentException('destintation endpoint must have single object filter options.filter_one as a string');
55
        }
56
57
        if (!is_array($resource['data']['resource'])) {
58
            throw new InvalidArgumentException('resource as array must be provided');
59
        }
60
61
        foreach ($resource['data']['options'] as $option => $value) {
62
            switch ($option) {
63
                case 'flush':
64
                    if (!is_bool($value)) {
65
                        throw new InvalidArgumentException('options.flush must be a boolean');
66
                    }
67
68
                break;
69
                case 'identifier':
70
                case 'import':
71
                break;
72
                case 'filter_all':
73
                case 'filter_one':
74
                    if (!is_string($value) && !is_null($value)) {
75
                        throw new InvalidArgumentException('options.'.$option.' must be a string');
76
                    }
77
78
                break;
79
                default:
80
                    throw new InvalidArgumentException('unknown option '.$option.' provided');
81
            }
82
        }
83
84
        return self::validateEndpoint($resource);
85
    }
86
87
    /**
88
     * Validate endpoint.
89
     */
90
    protected static function validateEndpoint(array $resource): array
91
    {
92
        $class = EndpointInterface::ENDPOINT_MAP[$resource['kind']];
93
        $validator = $class.'\\Validator';
94
        if (class_exists($validator)) {
95
            $resource['data'] = $validator::validate($resource['data']);
96
97
            return $resource;
98
        }
99
100
        return $resource;
101
    }
102
}
103