Passed
Push — master ( 67a2d6...dfe09a )
by Pieter
03:00
created

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