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

Validator::validate()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 36

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 10
CRAP Score 16.9832

Importance

Changes 0
Metric Value
dl 3
loc 36
ccs 10
cts 17
cp 0.5881
rs 7.6666
c 0
b 0
f 0
cc 10
nc 7
nop 1
crap 16.9832

How to fix   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\Resource;
13
14
use InvalidArgumentException;
15
16
class Validator
17
{
18
    /**
19
     * Validate resource.
20
     */
21 16
    public static function validate(array $resource): array
22
    {
23
        $defaults = [
24 16
            'data' => [],
25
        ];
26
27 16
        $resource = array_replace_recursive($defaults, $resource);
28
29 16
        if (!isset($resource['name']) || !is_string($resource['name'])) {
30
            throw new InvalidArgumentException('name as string must be provided');
31
        }
32
33 16
        if (preg_match('/[^a-z\.\-\_0-9]/', $resource['name'])) {
34
            throw new InvalidArgumentException('resoure name can only consists from lower case alphanumeric characters and . or _ or -');
35
        }
36
37 16
        if (isset($resource['description']) && !is_string($resource['description'])) {
38
            throw new InvalidArgumentException('description must be a string');
39
        }
40
41 16 View Code Duplication
        if (isset($resource['data']) && !is_array($resource['data'])) {
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...
42
            throw new InvalidArgumentException('data must be an array');
43
        }
44
45 16
        if (isset($resource['secrets'])) {
46
            if (!is_array($resource['secrets'])) {
47
                throw new InvalidArgumentException('secrets must be an array');
48
            }
49
50
            self::validateSecrets($resource['secrets']);
51
        }
52
53 16
        $resource = array_intersect_key($resource, array_flip(['name', 'data', 'description', 'secrets', 'kind']));
54
55 16
        return $resource;
56
    }
57
58
    /**
59
     * Validate secrets.
60
     */
61 View Code Duplication
    protected static function validateSecrets(array $secrets): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        foreach ($secrets as $key => $value) {
64
            if (['secret', 'key', 'to'] != array_keys($value)) {
65
                throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings');
66
            }
67
68
            if (array_filter($value, 'is_string') != $value) {
69
                throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings');
70
            }
71
        }
72
73
        return $secrets;
74
    }
75
}
76