Passed
Push — master ( 3c9073...c9c108 )
by Pieter
04:28
created

onPrePersistExistingResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace W2w\Laravel\Apie\Plugins\IlluminateTranslation;
4
5
use erasys\OpenApi\Spec\v3\Document;
6
use erasys\OpenApi\Spec\v3\Operation;
7
use erasys\OpenApi\Spec\v3\Parameter;
8
use Illuminate\Contracts\Translation\Translator;
9
use Illuminate\Foundation\Application;
10
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\Normalizers\LocationableExceptionNormalizer;
13
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\SubActions\TransChoiceSubAction;
14
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\ValueObjects\Locale;
15
use W2w\Lib\Apie\Events\DecodeEvent;
16
use W2w\Lib\Apie\Events\DeleteResourceEvent;
17
use W2w\Lib\Apie\Events\ModifySingleResourceEvent;
18
use W2w\Lib\Apie\Events\NormalizeEvent;
19
use W2w\Lib\Apie\Events\ResponseEvent;
20
use W2w\Lib\Apie\Events\RetrievePaginatedResourcesEvent;
21
use W2w\Lib\Apie\Events\RetrieveSingleResourceEvent;
22
use W2w\Lib\Apie\Events\StoreExistingResourceEvent;
23
use W2w\Lib\Apie\Events\StoreNewResourceEvent;
24
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface;
25
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait;
26
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface;
27
use W2w\Lib\Apie\PluginInterfaces\OpenApiEventProviderInterface;
28
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface;
29
use W2w\Lib\Apie\PluginInterfaces\SubActionsProviderInterface;
30
use W2w\Lib\Apie\Plugins\Core\Normalizers\ExceptionNormalizer;
31
32
class IlluminateTranslationPlugin implements ApieAwareInterface, OpenApiEventProviderInterface, ResourceLifeCycleInterface, SubActionsProviderInterface, NormalizerProviderInterface
33
{
34
    use ApieAwareTrait;
35
36
    /**
37
     * @var array
38
     */
39
    private $locales;
40
41
    /**
42
     * @var Translator
43
     */
44
    private $translator;
45
46
    /**
47
     * @var Application
48
     */
49
    private $application;
50
51
    /**
52
     * @param string[] $locales
53
     * @param Translator $translator
54
     */
55
    public function __construct(array $locales, Translator $translator, Application $application)
56
    {
57
        $this->locales = $locales;
58
        $this->translator = $translator;
59
        $this->application = $application;
60
        Locale::$locales = $this->locales;
61
    }
62
63
    public function onOpenApiDocGenerated(Document $document): Document
64
    {
65
        foreach ($document->paths as $path) {
66
            $this->patchOperation($path->get);
67
            $this->patchOperation($path->put);
68
            $this->patchOperation($path->post);
69
            $this->patchOperation($path->patch);
70
            $this->patchOperation($path->delete);
71
            $this->patchOperation($path->options);
72
            $this->patchOperation($path->head);
73
            $this->patchOperation($path->trace);
74
        }
75
        return $document;
76
    }
77
78
    private function patchOperation(?Operation $operation)
79
    {
80
        if ($operation === null) {
81
            return;
82
        }
83
        if (null === $operation->parameters) {
84
            $operation->parameters = [];
85
        }
86
        Locale::$locales = $this->locales;
87
        $operation->parameters[] = new Parameter(
88
            'Accept-Language',
89
            Parameter::IN_HEADER,
90
            'language',
91
            [
92
                'schema' => Locale::toSchema(),
93
            ]
94
        );
95
    }
96
97
    public function onPreDeleteResource(DeleteResourceEvent $event)
98
    {
99
    }
100
101
    public function onPostDeleteResource(DeleteResourceEvent $event)
102
    {
103
    }
104
105
    public function onPreRetrieveResource(RetrieveSingleResourceEvent $event)
106
    {
107
    }
108
109
    public function onPostRetrieveResource(RetrieveSingleResourceEvent $event)
110
    {
111
    }
112
113
    public function onPreRetrieveAllResources(RetrievePaginatedResourcesEvent $event)
114
    {
115
    }
116
117
    public function onPostRetrieveAllResources(RetrievePaginatedResourcesEvent $event)
118
    {
119
    }
120
121
    public function onPrePersistExistingResource(StoreExistingResourceEvent $event)
122
    {
123
    }
124
125
    public function onPostPersistExistingResource(StoreExistingResourceEvent $event)
126
    {
127
    }
128
129
    public function onPreDecodeRequestBody(DecodeEvent $event)
130
    {
131
    }
132
133
    public function onPostDecodeRequestBody(DecodeEvent $event)
134
    {
135
    }
136
137
    public function onPreModifyResource(ModifySingleResourceEvent $event)
138
    {
139
    }
140
141
    public function onPostModifyResource(ModifySingleResourceEvent $event)
142
    {
143
    }
144
145
    public function onPreCreateResource(StoreNewResourceEvent $event)
146
    {
147
    }
148
149
    public function onPostCreateResource(StoreNewResourceEvent $event)
150
    {
151
    }
152
153
    public function onPrePersistNewResource(StoreExistingResourceEvent $event)
154
    {
155
    }
156
157
    public function onPostPersistNewResource(StoreExistingResourceEvent $event)
158
    {
159
    }
160
161
    public function onPreCreateResponse(ResponseEvent $event)
162
    {
163
    }
164
165
    public function onPostCreateResponse(ResponseEvent $event)
166
    {
167
        $response = $event->getResponse();
168
        $locale = $this->application->getLocale();
169
        if ($locale && !$response->hasHeader('Content-Language')) {
170
            $event->setResponse($response->withAddedHeader('Content-Language', $this->application->getLocale()));
0 ignored issues
show
Bug introduced by
It seems like $response->withAddedHead...plication->getLocale()) can also be of type null; however, parameter $response of W2w\Lib\Apie\Events\ResponseEvent::setResponse() does only seem to accept Psr\Http\Message\ResponseInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
            $event->setResponse(/** @scrutinizer ignore-type */ $response->withAddedHeader('Content-Language', $this->application->getLocale()));
Loading history...
171
        }
172
173
    }
174
175
    public function onPreCreateNormalizedData(NormalizeEvent $event)
176
    {
177
    }
178
179
    public function onPostCreateNormalizedData(NormalizeEvent $event)
180
    {
181
    }
182
183
    public function getSubActions()
184
    {
185
        return [
186
            'withPlaceholders' => [new TransChoiceSubAction($this->translator)]
187
        ];
188
    }
189
190
    public function getNormalizers(): array
191
    {
192
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(new W2w\Lar...on\Translator::class))) returns the type array<integer,W2w\Larave...bleExceptionNormalizer> which is incompatible with the return type mandated by W2w\Lib\Apie\PluginInter...rface::getNormalizers() of Symfony\Component\Serial...DenormalizerInterface[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
193
            new LocationableExceptionNormalizer(
194
                new ExceptionNormalizer($this->getApie()->isDebug()),
195
                $this->application->make(Translator::class)
196
            ),
197
        ];
198
    }
199
}
200