Passed
Push — master ( cce2e1...993520 )
by Dawid
02:22
created

AbstractSchemaValidatorFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 63
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A registerSchema() 0 10 2
A hasSchema() 0 4 1
A assertHasSchema() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Service;
6
7
use Symfony\Component\Config\FileLocatorInterface;
8
9
abstract class AbstractSchemaValidatorFactory
10
{
11
    /**
12
     * @var FileLocatorInterface
13
     */
14
    protected $fileLocator;
15
16
    /**
17
     * @var array
18
     */
19
    protected $registeredSchemas;
20
21
    /**
22
     * @param FileLocatorInterface $fileLocator
23
     */
24 12
    public function __construct(FileLocatorInterface $fileLocator)
25
    {
26 12
        $this->fileLocator = $fileLocator;
27
28 12
        $this->registeredSchemas = [];
29 12
    }
30
31
    /**
32
     * @param string $id
33
     * @param string $schemaResourceLocation
34
     *
35
     * @throws \RuntimeException When schema with provided $id is already registered
36
     *
37
     * @return self
38
     */
39 3
    public function registerSchema(string $id, string $schemaResourceLocation): self
40
    {
41 3
        if ($this->hasSchema($id)) {
42
            throw new \RuntimeException(sprintf('Schema with id "%s" already registered.', $id));
43
        }
44
45 3
        $this->registeredSchemas[$id] = $schemaResourceLocation;
46
47 3
        return $this;
48
    }
49
50
    /**
51
     * @param string $id
52
     *
53
     * @return bool
54
     */
55 3
    public function hasSchema(string $id): bool
56
    {
57 3
        return array_key_exists($id, $this->registeredSchemas);
58
    }
59
60
    /**
61
     * @param string $id
62
     *
63
     * @throws \RuntimeException When schema with id "%s" is not registered
64
     */
65 3
    protected function assertHasSchema(string $id): void
66
    {
67 3
        if (!$this->hasSchema($id)) {
68
            throw new \RuntimeException(sprintf('Schema with id "%s" is not registered.', $id));
69
        }
70 3
    }
71
}
72