Completed
Push — master ( 578000...8d28bc )
by Dawid
07:57
created

ResponseSchemaValidator::getSchemas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1
    public function __construct(array $data)
26
    {
27 1
        if (empty($data)) {
28
            throw new \InvalidArgumentException('Empty schemas provided');
29
        }
30
31 1
        foreach ($data as $format => $schemas) {
32 1
            if (!is_string($format)) {
33
                throw new \InvalidArgumentException($format.' is not a string');
34
            }
35
36 1
            if (!is_array($schemas)) {
37
                throw new \InvalidArgumentException($schemas.' is not an array');
38
            }
39
40 1
            $this->loadFormatSchemas($format, $schemas);
41
        }
42 1
    }
43
44 1
    public function getSchemas(): array
45
    {
46 1
        return $this->schemas;
47
    }
48
49 1
    protected function loadFormatSchemas(string $format, array $schemas): void
50
    {
51 1
        $format = strtolower($format);
52 1
        $this->schemas[$format] = [];
53
54 1
        foreach ($schemas as $responseCode => $schema) {
55 1
            if (!is_int($responseCode)) {
56
                throw new \InvalidArgumentException($responseCode.' is not an integer');
57
            }
58
59 1
            if (!is_string($schema)) {
60
                throw new \InvalidArgumentException($schema.' is not a string');
61
            }
62
63 1
            $this->schemas[$format][$responseCode] = $schema;
64
        }
65 1
    }
66
}
67