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

Validator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 86
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 1
dl 6
loc 86
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D validate() 6 64 19
A validateEndpoint() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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