Completed
Push — master ( c34cac...3c68a7 )
by Pieter
04:47
created

ApieCore   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 128
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourcePersister() 0 8 2
A getResourceRetriever() 0 8 2
A __construct() 0 3 1
A getApiResourceFacade() 0 8 1
A getApiResourceMetadataFactory() 0 9 2
A getSchemaGenerator() 0 17 4
A getIdentifierExtractor() 0 6 2
A getOpenApiSpecGenerator() 0 12 1
A getClassResourceConverter() 0 6 1
1
<?php
2
3
namespace W2w\Lib\Apie\Core;
4
5
use erasys\OpenApi\Spec\v3\Document;
6
use W2w\Lib\Apie\Apie;
7
use W2w\Lib\Apie\Core\Resources\ApiResources;
8
use W2w\Lib\Apie\OpenApiSchema\OpenApiSpecGenerator;
9
use W2w\Lib\Apie\OpenApiSchema\SchemaGenerator;
10
11
/**
12
 * Used by Apie to create the general Apie classes which you are not supposed to override in a plugin.
13
 *
14
 * @internal
15
 */
16
class ApieCore
17
{
18
    /**
19
     * @var Apie
20
     */
21
    private $apie;
22
23
    /**
24
     * @var ApiResourceRetriever|null
25
     */
26
    private $retriever;
27
28
    /**
29
     * @var ApiResourcePersister|null
30
     */
31
    private $persister;
32
33
    /**
34
     * @var ApiResourceMetadataFactory|null
35
     */
36
    private $metadataFactory;
37
38
    /**
39
     * @var IdentifierExtractor|null
40
     */
41
    private $identifierExtractor;
42
43
    /**
44
     * @var SchemaGenerator|null
45
     */
46
    private $schemaGenerator;
47
48
    public function __construct(Apie $apie)
49
    {
50
        $this->apie = $apie;
51
    }
52
53
    public function getOpenApiSpecGenerator(): OpenApiSpecGenerator
54
    {
55
        return new OpenApiSpecGenerator(
56
            new ApiResources($this->apie->getResources()),
57
            $this->getClassResourceConverter(),
58
            $this->apie->createInfo(),
59
            $this->getSchemaGenerator(),
60
            $this->getApiResourceMetadataFactory(),
61
            $this->getIdentifierExtractor(),
62
            $this->apie->getBaseUrl(),
63
            function (Document $doc) {
64
                $this->apie->onOpenApiDocGenerated($doc);
65
            }
66
        );
67
    }
68
69
    public function getSchemaGenerator(): SchemaGenerator
70
    {
71
        if (!$this->schemaGenerator) {
72
            $this->schemaGenerator = new SchemaGenerator(
73
                $this->apie->getClassMetadataFactory(),
74
                $this->apie->getPropertyTypeExtractor(),
75
                $this->getClassResourceConverter(),
76
                $this->apie->getPropertyConverter()
77
            );
78
            foreach ($this->apie->getDefinedStaticData() as $class => $schema) {
79
                $this->schemaGenerator->defineSchemaForResource($class, $schema);
80
            }
81
            foreach ($this->apie->getDynamicSchemaLogic() as $class => $schema) {
82
                // TODO: make method in schema generator
83
            }
84
        }
85
        return $this->schemaGenerator;
86
    }
87
88
    public function getApiResourceFacade(): ApiResourceFacade
89
    {
90
        return new ApiResourceFacade(
91
            $this->getResourceRetriever(),
92
            $this->getResourcePersister(),
93
            $this->getClassResourceConverter(),
94
            $this->apie->getResourceSerializer(),
95
            $this->apie->getFormatRetriever()
96
        );
97
    }
98
99
    public function getResourceRetriever(): ApiResourceRetriever
100
    {
101
        if (!$this->retriever) {
102
            $this->retriever = new ApiResourceRetriever(
103
                $this->getApiResourceMetadataFactory()
104
            );
105
        }
106
        return $this->retriever;
107
    }
108
109
    public function getResourcePersister(): ApiResourcePersister
110
    {
111
        if (!$this->persister) {
112
            $this->persister = new ApiResourcePersister(
113
                $this->getApiResourceMetadataFactory()
114
            );
115
        }
116
        return $this->persister;
117
    }
118
119
    public function getApiResourceMetadataFactory(): ApiResourceMetadataFactory
120
    {
121
        if (!$this->metadataFactory) {
122
            $this->metadataFactory = new ApiResourceMetadataFactory(
123
                $this->apie->getAnnotationReader(),
124
                $this->apie->getApiResourceFactory()
125
            );
126
        }
127
        return $this->metadataFactory;
128
    }
129
130
    public function getIdentifierExtractor(): IdentifierExtractor
131
    {
132
        if (!$this->identifierExtractor) {
133
            $this->identifierExtractor = new IdentifierExtractor($this->apie->getPropertyAccessor());
134
        }
135
        return $this->identifierExtractor;
136
    }
137
138
    public function getClassResourceConverter(): ClassResourceConverter
139
    {
140
        return new ClassResourceConverter(
141
            $this->apie->getPropertyConverter(),
142
            new ApiResources($this->apie->getResources()),
143
            $this->apie->isDebug()
144
        );
145
    }
146
}
147