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

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 43
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 37 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\Ucs;
13
14
use InvalidArgumentException;
15
16
class Validator
17
{
18
    /**
19
     * Validate resource.
20
     */
21
    public static function validate(array $resource): array
22
    {
23
        $defaults = [
24
            'options' => [
25
                'identifier' => '$dn$',
26
            ],
27
            'resource' => [
28
                'request_options' => [],
29
                'auth' => [
30
                    'username' => null,
31
                    'password' => null,
32
                ],
33
            ],
34
        ];
35
36
        if (!isset($resource['resource']['base_uri']) || !is_string($resource['resource']['base_uri'])) {
37
            throw new InvalidArgumentException('resource.base_uri is required and must be a valid ucs url [string]');
38
        }
39
40
        if (!isset($resource['resource']['flavor']) || !is_string($resource['resource']['flavor'])) {
41
            throw new InvalidArgumentException('resource.flavor is required and must be a valid ucs flavor');
42
        }
43
44
        foreach ($resource['resource'] as $key => $value) {
45
            switch ($key) {
46
                case 'request_options':
47
                case 'flavor':
48
                case 'auth':
49
                case 'base_uri':
50
                break;
51
                default:
52
                    throw new InvalidArgumentException("unknown option resource.$key provided");
53
            }
54
        }
55
56
        return array_replace_recursive($defaults, $resource);
57
    }
58
}
59