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

Validator::validateSecrets()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 7
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
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