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

ResponseSchemaValidator::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 1
crap 30
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