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

ResponseSchemaValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 54
ccs 17
cts 22
cp 0.7727
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 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