Passed
Push — master ( dd2bba...2d5c28 )
by
unknown
04:10
created

IlluminatePlugin::getSubActions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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