processAnnotationForDtoClass()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
cc 6
nc 6
nop 1
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\Service\AnnotationProcessor;
14
15
use Doctrine\Common\Annotations\Reader;
16
use StfalconStudio\ApiBundle\Annotation\JsonSchema;
17
use StfalconStudio\ApiBundle\Annotation\JsonSchemaAnnotationInterface;
18
use StfalconStudio\ApiBundle\Exception\InvalidArgumentException;
19
use StfalconStudio\ApiBundle\Exception\RuntimeException;
20
use StfalconStudio\ApiBundle\Util\File\FileReader;
21
22
/**
23
 * JsonSchemaAnnotationProcessor.
24
 */
25
class JsonSchemaAnnotationProcessor
26
{
27
    /** @var DtoAnnotationProcessor */
28
    private $dtoAnnotationProcessor;
29
30
    /** @var Reader */
31
    private $annotationReader;
32
33
    /** @var FileReader */
34
    private $fileReader;
35
36
    /** @var string */
37
    private $jsonSchemaDir;
38
39
    /** @var array|string[] */
40
    private $cachedClasses = [];
41
42
    /**
43
     * @param DtoAnnotationProcessor $dtoAnnotationProcessor
44
     * @param FileReader             $fileReader
45
     * @param Reader                 $annotationReader
46
     * @param string                 $jsonSchemaDir
47
     */
48
    public function __construct(DtoAnnotationProcessor $dtoAnnotationProcessor, FileReader $fileReader, Reader $annotationReader, string $jsonSchemaDir)
49
    {
50
        $this->dtoAnnotationProcessor = $dtoAnnotationProcessor;
51
        $this->fileReader = $fileReader;
52
        $this->annotationReader = $annotationReader;
53
        $this->jsonSchemaDir = $jsonSchemaDir;
54
    }
55
56
    /**
57
     * @param string $controllerClassName
58
     *
59
     * @return mixed
60
     */
61
    public function processAnnotationForControllerClass(string $controllerClassName)
62
    {
63
        $dtoClass = $this->dtoAnnotationProcessor->processAnnotationForClass($controllerClassName);
64
65
        return $this->processAnnotationForDtoClass($dtoClass);
66
    }
67
68
    /**
69
     * @param string $dtoClassName
70
     *
71
     * @throws RuntimeException
72
     * @throws InvalidArgumentException
73
     *
74
     * @return mixed
75
     */
76
    public function processAnnotationForDtoClass(string $dtoClassName)
77
    {
78
        /** @var class-string<object> $dtoClassName */
0 ignored issues
show
Documentation introduced by
The doc-type class-string<object> could not be parsed: Unknown type name "class-string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
79
        if (\array_key_exists($dtoClassName, $this->cachedClasses)) {
80
            return $this->cachedClasses[$dtoClassName];
81
        }
82
83
        $classAnnotation = $this->annotationReader->getClassAnnotation(new \ReflectionClass($dtoClassName), JsonSchema::class);
84
85
        if (!$classAnnotation instanceof JsonSchemaAnnotationInterface) {
86
            throw new RuntimeException(\sprintf('Missing Json Schema annotation for class %s', $dtoClassName));
87
        }
88
89
        $jsonSchemaDirPath = \realpath($this->jsonSchemaDir);
90
        if (false === $jsonSchemaDirPath) {
91
            throw new RuntimeException(\sprintf('Directory for json Schema files "%s" is not found.', $this->jsonSchemaDir));
92
        }
93
94
        $path = $jsonSchemaDirPath.\DIRECTORY_SEPARATOR.$classAnnotation->getJsonSchemaFilename();
95
        $realPathToJsonSchemaFile = \realpath($path);
96
        if (false === $realPathToJsonSchemaFile) {
97
            throw new RuntimeException(\sprintf('Json Schema file "%s" is not found.', $path));
98
        }
99
100
        $jsonSchemaContent = $this->fileReader->getFileContents($realPathToJsonSchemaFile);
101
        if (false === $jsonSchemaContent) {
102
            throw new InvalidArgumentException(\sprintf('Cannot read content of file %s', $realPathToJsonSchemaFile));
103
        }
104
105
        $decodedSchema = \json_decode($jsonSchemaContent, true, 512, JSON_THROW_ON_ERROR);
106
107
        $this->cachedClasses[$dtoClassName] = $decodedSchema;
108
109
        return $decodedSchema;
110
    }
111
}
112