1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
4
|
|
|
|
5
|
|
|
use App\Composer\PackageImporter; |
6
|
|
|
use App\Document\Provider; |
7
|
|
|
use App\Mercure\ProviderPublisher; |
8
|
|
|
use App\Messenger\Message\ProviderImportation; |
9
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
10
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
11
|
|
|
use Nelmio\ApiDocBundle\Annotation\Security; |
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
13
|
|
|
use Swagger\Annotations as SWG; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
19
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
20
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Provide routes for provider API. |
24
|
|
|
* |
25
|
|
|
* @Route("/api/providers", name="api_provider_", defaults={"_format": "json"}) |
26
|
|
|
*/ |
27
|
|
|
class ProviderController extends AbstractController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* List the providers. |
31
|
|
|
* |
32
|
|
|
* @Route("", name="index", methods={"GET"}) |
33
|
|
|
* @SWG\Response( |
34
|
|
|
* response=200, |
35
|
|
|
* description="Returns the providers", |
36
|
|
|
* @SWG\Schema( |
37
|
|
|
* type="array", |
38
|
|
|
* @SWG\Items(ref=@Model(type=Provider::class, groups={"provider_default"})) |
39
|
|
|
* ) |
40
|
|
|
* ) |
41
|
|
|
* @SWG\Parameter( |
42
|
|
|
* name="page", |
43
|
|
|
* in="query", |
44
|
|
|
* type="integer", |
45
|
|
|
* description="Page number", |
46
|
|
|
* default="1" |
47
|
|
|
* ) |
48
|
|
|
* @SWG\Parameter( |
49
|
|
|
* name="query", |
50
|
|
|
* in="query", |
51
|
|
|
* type="string", |
52
|
|
|
* description="Search query", |
53
|
|
|
* ) |
54
|
|
|
* @SWG\Parameter( |
55
|
|
|
* name="type", |
56
|
|
|
* in="query", |
57
|
|
|
* type="string", |
58
|
|
|
* description="type filter", |
59
|
|
|
* ) |
60
|
|
|
* @SWG\Tag(name="providers") |
61
|
|
|
* @Security(name="Bearer") |
62
|
|
|
*/ |
63
|
|
|
public function list(DocumentManager $dm, Request $request): Response |
64
|
|
|
{ |
65
|
|
|
$page = $request->query->getInt('page', 1); |
66
|
|
|
$query = $request->query->get('query'); |
67
|
|
|
$type = $request->query->get('type'); |
68
|
|
|
|
69
|
|
|
$rs = [ |
70
|
|
|
'total' => $dm->getRepository('App:Provider')->count($query), |
71
|
|
|
'facets' => $dm->getRepository('App:Provider')->facets($query), |
72
|
|
|
'items' => $dm->getRepository('App:Provider')->search($page, $query, $type, 20), |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
return $this->json($rs, Response::HTTP_OK, [], ['groups' => ['provider_default']]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get a providers. |
80
|
|
|
* |
81
|
|
|
* @Route("/{name}", name="get", methods={"GET"}, requirements={"name": ".+"}) |
82
|
|
|
* @SWG\Response( |
83
|
|
|
* response=200, |
84
|
|
|
* description="Returns the provider", |
85
|
|
|
* @SWG\Schema(ref=@Model(type=Provider::class, groups={"provider_full", "package_default"}) |
86
|
|
|
* ) |
87
|
|
|
* ) |
88
|
|
|
* @SWG\Tag(name="providers") |
89
|
|
|
* @Security(name="Bearer") |
90
|
|
|
*/ |
91
|
|
|
public function find(Provider $provider): Response |
92
|
|
|
{ |
93
|
|
|
return $this->json($provider, Response::HTTP_OK, [], ['groups' => ['provider_full', 'package_default']]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create a provider. |
98
|
|
|
* |
99
|
|
|
* @IsGranted("ROLE_MANAGER") |
100
|
|
|
* |
101
|
|
|
* @Route("", name="create", methods={"POST"}) |
102
|
|
|
* @SWG\Response( |
103
|
|
|
* response=201, |
104
|
|
|
* description="Create a provider" |
105
|
|
|
* ) |
106
|
|
|
* @SWG\Response( |
107
|
|
|
* response=400, |
108
|
|
|
* description="Invalid provider" |
109
|
|
|
* ) |
110
|
|
|
* @SWG\Parameter( |
111
|
|
|
* name="body", |
112
|
|
|
* in="body", |
113
|
|
|
* description="JSON Payload", |
114
|
|
|
* required=true, |
115
|
|
|
* format="application/json", |
116
|
|
|
* @SWG\Schema( |
117
|
|
|
* type="object", |
118
|
|
|
* ref=@Model(type=Provider::class, groups={"provider_write"}) |
119
|
|
|
* ) |
120
|
|
|
* ) |
121
|
|
|
* @SWG\Tag(name="providers") |
122
|
|
|
* @Security(name="Bearer") |
123
|
|
|
*/ |
124
|
|
|
public function create( |
125
|
|
|
DocumentManager $dm, |
126
|
|
|
SerializerInterface $serializer, |
127
|
|
|
ValidatorInterface $validator, |
128
|
|
|
MessageBusInterface $bus, |
129
|
|
|
ProviderPublisher $publisher, |
130
|
|
|
Request $request): Response |
131
|
|
|
{ |
132
|
|
|
$data = $request->getContent(); |
133
|
|
|
|
134
|
|
|
/** @var Provider $provider */ |
135
|
|
|
$provider = $serializer->deserialize($data, Provider::class, 'json', ['groups' => ['provider_write']]); |
136
|
|
|
|
137
|
|
|
$violations = $validator->validate($provider); |
138
|
|
|
|
139
|
|
|
if ($violations->count() > 0) { |
140
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$provider->setUpdateInProgress(true); |
144
|
|
|
|
145
|
|
|
$dm->persist($provider); |
146
|
|
|
$dm->flush(); |
147
|
|
|
$publisher->publishProviderUpdate($provider); |
148
|
|
|
$bus->dispatch(new ProviderImportation($provider->getName())); |
149
|
|
|
|
150
|
|
|
return new Response('', Response::HTTP_CREATED); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Refresh a provider. |
155
|
|
|
* |
156
|
|
|
* @IsGranted("ROLE_MANAGER") |
157
|
|
|
* |
158
|
|
|
* @Route("/{name}", name="refresh", methods={"PUT"}, requirements={"name": ".+"}) |
159
|
|
|
* @SWG\Response( |
160
|
|
|
* response=204, |
161
|
|
|
* description="Refresh a provider" |
162
|
|
|
* ) |
163
|
|
|
* @SWG\Tag(name="providers") |
164
|
|
|
* @Security(name="Bearer") |
165
|
|
|
*/ |
166
|
|
|
public function refresh(DocumentManager $dm, Provider $provider, MessageBusInterface $bus, ProviderPublisher $publisher): Response |
167
|
|
|
{ |
168
|
|
|
$provider->setUpdateInProgress(true); |
169
|
|
|
$publisher->publishProviderUpdate($provider); |
170
|
|
|
$dm->flush(); |
171
|
|
|
$bus->dispatch(new ProviderImportation($provider->getName())); |
172
|
|
|
|
173
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Delete a provider. |
178
|
|
|
* |
179
|
|
|
* @IsGranted("ROLE_MANAGER") |
180
|
|
|
* |
181
|
|
|
* @Route("/{name}", name="delete", methods={"DELETE"}, requirements={"name": ".+"}) |
182
|
|
|
* @SWG\Response( |
183
|
|
|
* response=204, |
184
|
|
|
* description="Delete a provider" |
185
|
|
|
* ) |
186
|
|
|
* @SWG\Tag(name="providers") |
187
|
|
|
* @Security(name="Bearer") |
188
|
|
|
*/ |
189
|
|
|
public function delete(DocumentManager $dm, Provider $provider, PackageImporter $packageImporter): Response |
190
|
|
|
{ |
191
|
|
|
$dm->remove($provider); |
192
|
|
|
$dm->flush(); |
193
|
|
|
|
194
|
|
|
$packageImporter->updateReplaceProviderSignature($provider); |
195
|
|
|
$dm->flush(); |
196
|
|
|
|
197
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|