Completed
Push — master ( 05c9d0...7a0aab )
by Dawid
33:52 queued 30:56
created

AbstractSchemaValidatorFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasSchema() 0 3 1
A assertHasSchema() 0 4 2
A registerSchema() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Service\SchemaValidator;
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 8
    public function __construct(FileLocatorInterface $fileLocator)
25
    {
26 8
        $this->fileLocator = $fileLocator;
27
28 8
        $this->registeredSchemas = [];
29 8
    }
30
31
    /**
32
     * @param string $id
33
     * @param string $schemaResourceLocation
34
     *
35
     * @throws \RuntimeException When schema with provided $id is already registered
36
     */
37 7
    public function registerSchema(string $id, string $schemaResourceLocation): void
38
    {
39 7
        if ($this->hasSchema($id)) {
40 1
            throw new \RuntimeException(sprintf('Schema with id "%s" already registered.', $id));
41
        }
42
43 7
        $this->registeredSchemas[$id] = $schemaResourceLocation;
44 7
    }
45
46
    /**
47
     * @param string $id
48
     *
49
     * @return bool
50
     */
51 8
    public function hasSchema(string $id): bool
52
    {
53 8
        return array_key_exists($id, $this->registeredSchemas);
54
    }
55
56
    /**
57
     * @param string $id
58
     *
59
     * @throws \RuntimeException When schema with id "%s" is not registered
60
     */
61 7
    protected function assertHasSchema(string $id): void
62
    {
63 7
        if (!$this->hasSchema($id)) {
64 1
            throw new \RuntimeException(sprintf('Schema with id "%s" is not registered.', $id));
65
        }
66 6
    }
67
}
68