JsonSchema::getJsonSchemaFilename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the StfalconApiBundle.
4
 *
5
 * (c) Stfalcon LLC <stfalcon.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace StfalconStudio\ApiBundle\Annotation;
14
15
use Doctrine\Common\Annotations\Annotation;
16
use StfalconStudio\ApiBundle\Exception\LogicException;
17
18
/**
19
 * JsonSchema Annotation.
20
 *
21
 * @Annotation
22
 *
23
 * @Target({"CLASS"})
24
 */
25
class JsonSchema implements JsonSchemaAnnotationInterface
26
{
27
    private const JSON_FILE_EXTENSION = '.json';
28
29
    /** @var string */
30
    private $jsonSchemaName;
31
32
    /**
33
     * @param mixed[] $options
34
     */
35
    public function __construct(array $options)
36
    {
37
        $options = $this->assertJsonSchemaFileIsSet($options);
38
39
        $this->jsonSchemaName = $options['value'];
40
41
        $this->getJsonSchemaFilename(); // if file not found exception will be thrown
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getJsonSchemaFilename(): string
48
    {
49
        $result = $this->jsonSchemaName;
50
51
        if (self::JSON_FILE_EXTENSION !== \mb_substr($result, -5)) {
52
            $result .= self::JSON_FILE_EXTENSION;
53
        }
54
55
        return $result;
56
    }
57
58
    /**
59
     * @param mixed[] $options
60
     *
61
     * @throws LogicException
62
     *
63
     * @return array
64
     */
65
    private function assertJsonSchemaFileIsSet(array $options): array
66
    {
67
        if (!\array_key_exists('value', $options)) {
68
            throw new LogicException('Json Schema file must be set.');
69
        }
70
71
        return $options;
72
    }
73
}
74