IlluminatePlugin::onOpenApiDocGenerated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace W2w\Laravel\Apie\Plugins\Illuminate;
3
4
use erasys\OpenApi\Spec\v3\Contact;
5
use erasys\OpenApi\Spec\v3\Document;
6
use erasys\OpenApi\Spec\v3\Info;
7
use erasys\OpenApi\Spec\v3\License;
8
use erasys\OpenApi\Spec\v3\Schema;
9
use Illuminate\Auth\Authenticatable;
10
use Illuminate\Container\Container;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Collection;
14
use Illuminate\Support\LazyCollection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\LazyCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Serializer\Encoder\EncoderInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17
use W2w\Laravel\Apie\Events\OpenApiSpecGenerated;
18
use W2w\Laravel\Apie\Plugins\Illuminate\Encoders\DefaultContentTypeFormatRetriever;
19
use W2w\Laravel\Apie\Plugins\Illuminate\Normalizers\CollectionNormalizer;
20
use W2w\Laravel\Apie\Plugins\Illuminate\Normalizers\LazyCollectionNormalizer;
21
use W2w\Laravel\Apie\Plugins\Illuminate\ObjectAccess\AuthObjectAccess;
22
use W2w\Laravel\Apie\Plugins\Illuminate\ResourceFactories\FromIlluminateContainerFactory;
23
use W2w\Laravel\Apie\Plugins\Illuminate\Schema\CollectionSchemaBuilder;
24
use W2w\Laravel\Apie\Providers\ApieConfigResolver;
25
use W2w\Lib\Apie\Core\Resources\ApiResourcesInterface;
26
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException;
27
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface;
28
use W2w\Lib\Apie\Interfaces\FormatRetrieverInterface;
29
use W2w\Lib\Apie\PluginInterfaces\ApieConfigInterface;
30
use W2w\Lib\Apie\PluginInterfaces\ApiResourceFactoryProviderInterface;
31
use W2w\Lib\Apie\PluginInterfaces\EncoderProviderInterface;
32
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface;
33
use W2w\Lib\Apie\PluginInterfaces\ObjectAccessProviderInterface;
34
use W2w\Lib\Apie\PluginInterfaces\OpenApiEventProviderInterface;
35
use W2w\Lib\Apie\PluginInterfaces\OpenApiInfoProviderInterface;
36
use W2w\Lib\Apie\PluginInterfaces\ResourceProviderInterface;
37
use W2w\Lib\Apie\PluginInterfaces\SchemaProviderInterface;
38
use W2w\Lib\Apie\PluginInterfaces\SubActionsProviderInterface;
39
use W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess\ObjectAccessInterface;
40
41
class IlluminatePlugin implements ObjectAccessProviderInterface, ResourceProviderInterface, ApieConfigInterface, OpenApiInfoProviderInterface, ApiResourceFactoryProviderInterface, EncoderProviderInterface, NormalizerProviderInterface, OpenApiEventProviderInterface, SchemaProviderInterface, SubActionsProviderInterface
42
{
43
    private $container;
44
45
    private $resolvedConfig;
46
47
    public function __construct(Container $container, array $config)
48
    {
49
        $this->container = $container;
50
        $this->resolvedConfig = ApieConfigResolver::resolveConfig($config);
51
    }
52
53
    public function getLaravelConfig(): array
54
    {
55
        return $this->resolvedConfig;
56
    }
57
58
    /**
59
     * Returns a list of Api resources.
60
     *
61
     * @return string[]
62
     */
63
    public function getResources(): array
64
    {
65
        if (!empty($this->resolvedConfig['resources-service'])) {
66
            $resources = $this->container->make($this->resolvedConfig['resources-service']);
67
            if (!($resources instanceof ApiResourcesInterface)) {
68
                throw new InvalidClassTypeException('resources-service', ApiResourcesInterface::class);
69
            }
70
            return $resources->getApiResources();
71
        }
72
        return $this->resolvedConfig['resources'];
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function getBaseUrl(): string
79
    {
80
        $baseUrl = $this->resolvedConfig['base-url'] . $this->resolvedConfig['api-url'];
81
        if ($this->container->has(Request::class)) {
82
            $baseUrl = $this->container->get(Request::class)->getSchemeAndHttpHost() . $baseUrl;
83
        }
84
        return $baseUrl;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function createInfo(): Info
91
    {
92
        return new Info(
93
            $this->resolvedConfig['metadata']['title'],
94
            $this->resolvedConfig['metadata']['version'],
95
            $this->resolvedConfig['metadata']['description'],
96
            [
97
                'contact' => new Contact([
98
                    'name'  => $this->resolvedConfig['metadata']['contact-name'],
99
                    'url'   => $this->resolvedConfig['metadata']['contact-url'],
100
                    'email' => $this->resolvedConfig['metadata']['contact-email'],
101
                ]),
102
                'license' => new License(
103
                    $this->resolvedConfig['metadata']['license'],
104
                    $this->resolvedConfig['metadata']['license-url']
105
                ),
106
            ]
107
        );
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function getApiResourceFactory(): ApiResourceFactoryInterface
114
    {
115
        return new FromIlluminateContainerFactory($this->container);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function getEncoders(): array
122
    {
123
        $res = [];
124
        // container->tagged has hazy return value...
125
        foreach ($this->container->tagged(EncoderInterface::class) as $normalizer) {
126
            $res[] = $normalizer;
127
        };
128
        return $res;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function getFormatRetriever(): FormatRetrieverInterface
135
    {
136
        return new DefaultContentTypeFormatRetriever();
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function getNormalizers(): array
143
    {
144
        $res = [];
145
        // container->tagged has hazy return value...
146
        foreach ($this->container->tagged(NormalizerInterface::class) as $normalizer) {
147
            $res[] = $normalizer;
148
        };
149
        $res[] = new CollectionNormalizer();
150
        if (class_exists(LazyCollection::class)) {
151
            $res[] = new LazyCollectionNormalizer();
152
        }
153
        return $res;
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function onOpenApiDocGenerated(Document $document): Document
160
    {
161
        $event = new OpenApiSpecGenerated($document);
162
        $this->container->get('events')->dispatch($event);
163
        return $event->getDocument();
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169
    public function getDefinedStaticData(): array
170
    {
171
        return [
172
            Model::class => new Schema([
173
                'type' => 'object'
174
            ]),
175
        ];
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function getDynamicSchemaLogic(): array
182
    {
183
        $schemas = [
184
            Collection::class => new CollectionSchemaBuilder(),
185
        ];
186
        if (class_exists(LazyCollection::class)) {
187
            $schemas[LazyCollection::class] = new LazyCollectionNormalizer();
188
        }
189
        return $schemas;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195
    public function getSubActions()
196
    {
197
        $subActions = $this->resolvedConfig['subactions'];
198
        $results = [];
199
        foreach ($subActions as $slug => $actions) {
200
            $results[$slug] = [];
201
            foreach ($actions as $action) {
202
                $results[$slug][] = $this->container->make($action);
203
            }
204
        }
205
        return $results;
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211
    public function getObjectAccesses(): array
212
    {
213
        $objectAccess = $this->resolvedConfig['object-access'];
214
        $results = [
215
            Authenticatable::class => new AuthObjectAccess(),
216
        ];
217
        foreach ($objectAccess as $key => $objectAccessClass) {
218
            $service = $this->container->make($objectAccessClass);
219
            if (!($service instanceof ObjectAccessInterface)) {
220
                throw new InvalidClassTypeException($objectAccessClass, 'ObjectAccessInterface');
221
            }
222
            $results[$key] = $service;
223
        }
224
        return $results;
225
    }
226
}
227