ResponseSchemaValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
c 2
b 0
f 1
dl 0
loc 60
ccs 21
cts 21
cp 1
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFormatSchemas() 0 15 4
A __construct() 0 16 5
A getSchemas() 0 3 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 10
    public function __construct(array $data)
26
    {
27 10
        if (empty($data)) {
28 1
            throw new \InvalidArgumentException('Empty schemas provided');
29
        }
30
31 9
        foreach ($data as $format => $schemas) {
32 9
            if (!\is_string($format)) {
33 1
                throw new \InvalidArgumentException($format.' is not a string');
34
            }
35
36 8
            if (!\is_array($schemas)) {
37 1
                throw new \InvalidArgumentException($schemas.' is not an array');
38
            }
39
40 7
            $this->loadFormatSchemas($format, $schemas);
41
        }
42 5
    }
43
44
    /**
45
     * @return array
46
     */
47 20
    public function getSchemas(): array
48
    {
49 20
        return $this->schemas;
50
    }
51
52
    /**
53
     * @param string $format
54
     * @param array  $schemas
55
     *
56
     * @throws \InvalidArgumentException
57
     */
58 7
    protected function loadFormatSchemas(string $format, array $schemas): void
59
    {
60 7
        $format = strtolower($format);
61 7
        $this->schemas[$format] = [];
62
63 7
        foreach ($schemas as $responseCode => $schema) {
64 7
            if (!\is_int($responseCode)) {
65 1
                throw new \InvalidArgumentException($responseCode.' is not an integer');
66
            }
67
68 6
            if (!\is_string($schema)) {
69 1
                throw new \InvalidArgumentException($schema.' is not a string');
70
            }
71
72 5
            $this->schemas[$format][$responseCode] = $schema;
73
        }
74 5
    }
75
}
76