Passed
Push — master ( 06fb26...109dc7 )
by Paweł
02:17
created

SchemaFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Util;
10
11
use Cake\Collection\Collection;
12
use Cake\Collection\CollectionInterface;
13
use OpenApi\Annotations\Items;
14
use OpenApi\Annotations\Property;
15
use OpenApi\Annotations\Schema;
16
use const OpenApi\Annotations\UNDEFINED;
17
18
final class SchemaFactory
19
{
20
    /** @var Collection<int, Schema> */
21
    private Collection $schemas;
22
23
    public function __construct(OAReader $oaReader)
24
    {
25
        $this->schemas = new Collection($oaReader->read()->components->schemas);
26
    }
27
28
    /**
29
     * Creates single JSON schema for provided class name
30
     */
31
    public function create(string $className): string
32
    {
33
        $schemaName = (new \ReflectionClass($className))->getShortName();
34
35
        $schema = $this->schemas->filter(
36
            static function(Schema $schema) use ($schemaName): bool {
37
                return $schema->schema === $schemaName;
38
            }
39
        )->first();
40
41
        $relatedSchemas = $this->getRelatedSchemas($schema)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type null; however, parameter $schema of Gorynych\Util\SchemaFactory::getRelatedSchemas() does only seem to accept OpenApi\Annotations\Schema, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $relatedSchemas = $this->getRelatedSchemas(/** @scrutinizer ignore-type */ $schema)->toArray();
Loading history...
42
        $schema = json_decode(json_encode($schema), true);
43
        $schema['components']['schemas'] = $relatedSchemas;
44
45
        return json_encode($schema);
46
    }
47
48
    /**
49
     * @return CollectionInterface<int, Schema>
50
     */
51
    private function getRelatedSchemas(Schema $schema): CollectionInterface
52
    {
53
        $schemas = $this->schemas;
54
        $relatedSchemas = new Collection([]);
55
56
        (new Collection($schema->properties))
57
            ->reject(
58
                static function(Property $property): bool {
59
                    /** @phpstan-ignore-next-line */
60
                    return UNDEFINED === $property->ref && UNDEFINED === $property->items;
61
                }
62
            )
63
            ->each(
64
                static function(Property $property) use ($schemas, &$relatedSchemas): void {
65
                    /** @phpstan-ignore-next-line */
66
                    $ref = $property->items instanceof Items ? $property->items->ref : $property->ref;
0 ignored issues
show
introduced by
$property->items is always a sub-type of OpenApi\Annotations\Items.
Loading history...
67
68
                    $relatedSchemas = $relatedSchemas->append(
69
                        $schemas->filter(
70
                            static function(Schema $schema) use ($ref): bool {
71
                                $ref = explode('/', $ref);
72
                                return $schema->schema === end($ref);
73
                            }
74
                        )
75
                    );
76
                }
77
            );
78
79
        return $relatedSchemas->indexBy(
80
            static function(Schema $schema): string {
81
                return $schema->schema;
82
            }
83
        );
84
    }
85
}
86