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

onPreRetrieveResource()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace W2w\Laravel\Apie\Plugins\IlluminateDispatcher;
5
6
use Illuminate\Auth\AuthManager;
7
use Illuminate\Contracts\Auth\Access\Gate;
8
use Illuminate\Contracts\Events\Dispatcher;
9
use Illuminate\Validation\Factory;
10
use Psr\Container\ContainerInterface;
11
use W2w\Lib\Apie\Events\DeleteResourceEvent;
12
use W2w\Lib\Apie\Events\ModifySingleResourceEvent;
13
use W2w\Lib\Apie\Events\NormalizeEvent;
14
use W2w\Lib\Apie\Events\ResponseEvent;
15
use W2w\Lib\Apie\Events\RetrievePaginatedResourcesEvent;
16
use W2w\Lib\Apie\Events\RetrieveSingleResourceEvent;
17
use W2w\Lib\Apie\Events\StoreExistingResourceEvent;
18
use W2w\Lib\Apie\Events\StoreNewResourceEvent;
19
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface;
20
21
/**
22
 * Link the Apie resource life cycle methods Laravel:
23
 * - event dispatcher
24
 * - validation rules with adding 'rules' in context.
25
 * - if the api resource is linked to a policy, check the policy.
26
 */
27
class IlluminateDispatcherPlugin implements ResourceLifeCycleInterface
28
{
29
    /**
30
     * @var Dispatcher
31
     */
32
    private $dispatcher;
33
34
    /**
35
     * @var Factory
36
     */
37
    private $validator;
38
39
    /**
40
     * @var AuthManager
41
     */
42
    private $gate;
43
44
    /**
45
     * @param Dispatcher $dispatcher
46
     * @param Factory $validator
47
     * @param Gate $gate
48
     */
49
    public function __construct(Dispatcher $dispatcher, Factory $validator, Gate $gate)
50
    {
51
        $this->dispatcher = $dispatcher;
52
        $this->validator = $validator;
53
        $this->gate = $gate;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gate of type Illuminate\Contracts\Auth\Access\Gate is incompatible with the declared type Illuminate\Auth\AuthManager of property $gate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function onPreDeleteResource(DeleteResourceEvent $event)
60
    {
61
        $this->dispatcher->dispatch($event);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function onPostDeleteResource(DeleteResourceEvent $event)
68
    {
69
        $this->dispatcher->dispatch($event);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function onPreRetrieveResource(RetrieveSingleResourceEvent $event)
76
    {
77
        $this->dispatcher->dispatch($event);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function onPostRetrieveResource(RetrieveSingleResourceEvent $event)
84
    {
85
        if ($this->gate->getPolicyFor($event->getResource())) {
86
            $this->gate->authorize('view', $event->getResource());
87
        }
88
89
        $this->dispatcher->dispatch($event);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function onPreRetrieveAllResources(RetrievePaginatedResourcesEvent $event)
96
    {
97
        $this->dispatcher->dispatch($event);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function onPostRetrieveAllResources(RetrievePaginatedResourcesEvent $event)
104
    {
105
        $resources = $event->getResources();
106
107
        $event->setResources($this->iterateList($resources));
0 ignored issues
show
Bug introduced by
It seems like $resources can also be of type null; however, parameter $resourceList of W2w\Laravel\Apie\Plugins...erPlugin::iterateList() does only seem to accept iterable, 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

107
        $event->setResources($this->iterateList(/** @scrutinizer ignore-type */ $resources));
Loading history...
108
        $this->dispatcher->dispatch($event);
109
    }
110
111
    private function iterateList(iterable $resourceList)
112
    {
113
        foreach ($resourceList as $resource) {
114
            // do policy check for every individual item because of polymorphic item list.
115
            if (!$this->gate->getPolicyFor($resource) || $this->gate->allows('view', $resource)) {
116
                yield $resource;
117
            }
118
        }
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function onPrePersistExistingResource(StoreExistingResourceEvent $event)
125
    {
126
        if ($this->gate->getPolicyFor($event->getResource())) {
127
            $this->gate->authorize('update', $event->getResource());
128
        }
129
        $this->dispatcher->dispatch($event);
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135
    public function onPostPersistExistingResource(StoreExistingResourceEvent $event)
136
    {
137
        $this->dispatcher->dispatch($event);
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143
    public function onPreModifyResource(ModifySingleResourceEvent $event)
144
    {
145
        $this->dispatcher->dispatch($event);
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151
    public function onPostModifyResource(ModifySingleResourceEvent $event)
152
    {
153
        $this->dispatcher->dispatch($event);
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function onPreCreateResource(StoreNewResourceEvent $event)
160
    {
161
        $this->dispatcher->dispatch($event);
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167
    public function onPostCreateResource(StoreNewResourceEvent $event)
168
    {
169
        $this->dispatcher->dispatch($event);
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175
    public function onPrePersistNewResource(StoreExistingResourceEvent $event)
176
    {
177
        if ($this->gate->getPolicyFor($event->getResource())) {
178
            $this->gate->authorize('create', $event->getResource());
179
        }
180
        $this->dispatcher->dispatch($event);
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186
    public function onPostPersistNewResource(StoreExistingResourceEvent $event)
187
    {
188
        $this->dispatcher->dispatch($event);
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194
    public function onPreCreateResponse(ResponseEvent $event)
195
    {
196
        $this->dispatcher->dispatch($event);
197
    }
198
199
    /**
200
     * {@inheritDoc}
201
     */
202
    public function onPostCreateResponse(ResponseEvent $event)
203
    {
204
        $this->dispatcher->dispatch($event);
205
    }
206
207
    /**
208
     * {@inheritDoc}
209
     */
210
    public function onPreCreateNormalizedData(NormalizeEvent $event)
211
    {
212
        $this->dispatcher->dispatch($event);
213
    }
214
215
    /**
216
     * {@inheritDoc}
217
     */
218
    public function onPostCreateNormalizedData(NormalizeEvent $event)
219
    {
220
        $this->dispatcher->dispatch($event);
221
    }
222
}
223