Completed
Branch master (18fbaa)
by Dawid
02:11 queued 23s
created

ResponseSchemaValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 18 5
A getSchemas() 0 4 1
A loadFormatSchemas() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Annotation\Controller;
6
7
use Doctrine\Common\Annotations\Annotation\Target;
8
9
/**
10
 * @Annotation
11
 * @Target({"METHOD"})
12
 */
13
class ResponseSchemaValidator
14
{
15
    /**
16
     * @var array [string format => [int responseCode => string pathToSchemaFile]]
17
     */
18
    protected $schemas = [];
19
20
    /**
21
     * @param array $data [string format => [int responseCode => string pathToSchemaFile]]
22
     *
23
     * @throws \InvalidArgumentException
24
     */
25
    public function __construct(array $data)
26
    {
27
        if (empty($data)) {
28
            throw new \InvalidArgumentException('Empty schemas provided');
29
        }
30
31
        foreach ($data as $format => $schemas) {
32
            if (!is_string($format)) {
33
                throw new \InvalidArgumentException($format.' is not a string');
34
            }
35
36
            if (!is_array($schemas)) {
37
                throw new \InvalidArgumentException($schemas.' is not an array');
38
            }
39
40
            $this->loadFormatSchemas($format, $schemas);
41
        }
42
    }
43
44
    public function getSchemas(): array
45
    {
46
        return $this->schemas;
47
    }
48
49
    protected function loadFormatSchemas(string $format, array $schemas): void
50
    {
51
        $format = strtolower($format);
52
        $this->schemas[$format] = [];
53
54
        foreach ($schemas as $responseCode => $schema) {
55
            if (!is_int($responseCode)) {
56
                throw new \InvalidArgumentException($responseCode.' is not an integer');
57
            }
58
59
            if (!is_string($schema)) {
60
                throw new \InvalidArgumentException($schema.' is not a string');
61
            }
62
63
            $this->schemas[$format][$responseCode] = $schema;
64
        }
65
    }
66
}
67