Passed
Push — master ( 0ce3b5...f76703 )
by Dawid
03:41
created

JsonSchemaValidatorFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 74
ccs 24
cts 26
cp 0.9231
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A registerSchema() 0 10 2
A hasSchema() 0 4 1
A getValidator() 0 12 3
A createSchema() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Service;
6
7
use JsonSchema\SchemaStorage;
8
use JsonSchema\Uri\UriRetriever;
9
use JsonSchema\Validator;
10
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
11
use Symfony\Component\Config\FileLocatorInterface;
12
13
class JsonSchemaValidatorFactory
14
{
15
    /**
16
     * @var FileLocatorInterface
17
     */
18
    protected $fileLocator;
19
20
    /**
21
     * @var array
22
     */
23
    protected $registeredSchemas;
24
25 1
    public function __construct(FileLocatorInterface $fileLocator)
26
    {
27 1
        $this->fileLocator = $fileLocator;
28 1
        $this->registeredSchemas = [];
29 1
    }
30
31
    /**
32
     * @throws \RuntimeException When schema with provided $id is already registered
33
     */
34 1
    public function registerSchema(string $id, string $schemaResourceLocation): self
35
    {
36 1
        if ($this->hasSchema($id)) {
37
            throw new \RuntimeException(sprintf('Schema with id "%s" already registered.', $id));
38
        }
39
40 1
        $this->registeredSchemas[$id] = $schemaResourceLocation;
41
42 1
        return $this;
43
    }
44
45 1
    public function hasSchema(string $id): bool
46
    {
47 1
        return array_key_exists($id, $this->registeredSchemas);
48
    }
49
50
    /**
51
     * @throws FileLocatorFileNotFoundException
52
     * @throws \RuntimeException
53
     * @throws \InvalidArgumentException
54
     */
55 1
    public function getValidator(string $id): JsonSchemaValidator
56
    {
57 1
        if (!$this->hasSchema($id)) {
58
            throw new \RuntimeException(sprintf('Schema with id "%s" is not registered.', $id));
59
        }
60
61 1
        if (!$this->registeredSchemas[$id] instanceof JsonSchemaValidator) {
62 1
            $this->registeredSchemas[$id] = new JsonSchemaValidator($this->createSchema($id), new Validator());
63
        }
64
65 1
        return $this->registeredSchemas[$id];
66
    }
67
68
    /**
69
     * @throws FileLocatorFileNotFoundException
70
     * @throws \InvalidArgumentException
71
     */
72 1
    protected function createSchema(string $id): \stdClass
73
    {
74 1
        $mainSchemaFilePath = $this->fileLocator->locate($this->registeredSchemas[$id]);
75 1
        $schemaUri = basename($mainSchemaFilePath);
76 1
        $schemaBaseUri = sprintf('file://%s/%s', dirname($mainSchemaFilePath), $schemaUri);
77
78 1
        $retriever = new UriRetriever();
79 1
        $schema = $retriever->retrieve($schemaUri, $schemaBaseUri);
80
81 1
        $schemaStorage = new SchemaStorage($retriever);
82 1
        $schemaStorage->addSchema($schemaBaseUri, $schema);
83
84 1
        return $schema;
85
    }
86
}
87