Passed
Push — master ( c5bd37...640ffc )
by
unknown
05:22
created

IlluminatePlugin::getNormalizers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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