|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Controller; |
|
18
|
|
|
|
|
19
|
|
|
use Doctrine\Common\Collections\Collection; |
|
20
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
23
|
|
|
use SWP\Bundle\CoreBundle\Form\Type\CompositePublishActionType; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Form\Type\PackageType; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Form\Type\UnpublishFromTenantsType; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\Model\CompositePublishAction; |
|
27
|
|
|
use SWP\Bundle\CoreBundle\Model\PackageInterface; |
|
28
|
|
|
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents; |
|
29
|
|
|
use SWP\Component\Bridge\Model\ContentInterface; |
|
30
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
31
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
|
32
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
|
33
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
|
34
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
|
35
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
37
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
38
|
|
|
|
|
39
|
|
|
class PackageController extends Controller |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* List all items. |
|
43
|
|
|
* |
|
44
|
|
|
* @ApiDoc( |
|
45
|
|
|
* resource=true, |
|
46
|
|
|
* description="List all packages", |
|
47
|
|
|
* statusCodes={ |
|
48
|
|
|
* 200="Returned on success.", |
|
49
|
|
|
* 500="Returned when unexpected error occurred." |
|
50
|
|
|
* }, |
|
51
|
|
|
* filters={ |
|
52
|
|
|
* {"name"="status", "dataType"="string", "pattern"="published|unpublished|new|canceled"}, |
|
53
|
|
|
* {"name"="sorting", "dataType"="string", "pattern"="[updatedAt]=asc|desc"} |
|
54
|
|
|
* } |
|
55
|
|
|
* ) |
|
56
|
|
|
* @Route("/api/{version}/packages/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_packages") |
|
57
|
|
|
* @Method("GET") |
|
58
|
|
|
*/ |
|
59
|
|
|
public function listAction(Request $request) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
62
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
63
|
|
|
|
|
64
|
|
|
$packages = $this->getPackageRepository() |
|
65
|
|
|
->getPaginatedByCriteria(new Criteria([ |
|
66
|
|
|
'organization' => $tenantContext->getTenant()->getOrganization()->getId(), |
|
67
|
|
|
'status' => $request->query->get('status', ''), |
|
68
|
|
|
]), $request->query->get('sorting', []), new PaginationData($request)); |
|
69
|
|
|
|
|
70
|
|
|
return new ResourcesListResponse($packages); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Show single package. |
|
75
|
|
|
* |
|
76
|
|
|
* @ApiDoc( |
|
77
|
|
|
* resource=true, |
|
78
|
|
|
* description="Show single package", |
|
79
|
|
|
* statusCodes={ |
|
80
|
|
|
* 200="Returned on success." |
|
81
|
|
|
* } |
|
82
|
|
|
* ) |
|
83
|
|
|
* @Route("/api/{version}/packages/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_show_package", requirements={"id"="\d+"}) |
|
84
|
|
|
* @Method("GET") |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getAction(int $id) |
|
87
|
|
|
{ |
|
88
|
|
|
return new SingleResourceResponse($this->findOr404($id)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Publishes package to many websites. |
|
93
|
|
|
* |
|
94
|
|
|
* @ApiDoc( |
|
95
|
|
|
* resource=true, |
|
96
|
|
|
* description="Publishes package to many tenants", |
|
97
|
|
|
* statusCodes={ |
|
98
|
|
|
* 200="Returned on success.", |
|
99
|
|
|
* 400="Returned when validation failed.", |
|
100
|
|
|
* 500="Returned when unexpected error." |
|
101
|
|
|
* }, |
|
102
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\CompositePublishActionType" |
|
103
|
|
|
* ) |
|
104
|
|
|
* @Route("/api/{version}/packages/{id}/publish/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_publish_package", requirements={"id"="\d+"}) |
|
105
|
|
|
* |
|
106
|
|
|
* @Method("POST") |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function publishAction(Request $request, int $id) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
111
|
|
|
/** @var PackageInterface $package */ |
|
112
|
|
|
$package = $this->findOr404($id); |
|
113
|
|
|
|
|
114
|
|
|
$form = $this->createForm(CompositePublishActionType::class, new CompositePublishAction(), ['method' => $request->getMethod()]); |
|
115
|
|
|
$form->handleRequest($request); |
|
116
|
|
|
|
|
117
|
|
|
if ($form->isValid()) { |
|
118
|
|
|
$this->get('swp_core.article.publisher')->publish($package, $form->getData()); |
|
119
|
|
|
$this->get('fos_elastica.object_persister.swp.package')->replaceOne($package); |
|
120
|
|
|
|
|
121
|
|
|
return new SingleResourceResponse(null, new ResponseContext(201)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Un-publishes package from many websites. |
|
129
|
|
|
* |
|
130
|
|
|
* @ApiDoc( |
|
131
|
|
|
* resource=true, |
|
132
|
|
|
* description="Un-publishes package from many tenants", |
|
133
|
|
|
* statusCodes={ |
|
134
|
|
|
* 200="Returned on success.", |
|
135
|
|
|
* 400="Returned when validation failed.", |
|
136
|
|
|
* 500="Returned when unexpected error." |
|
137
|
|
|
* }, |
|
138
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\UnpublishFromTenantsType" |
|
139
|
|
|
* ) |
|
140
|
|
|
* @Route("/api/{version}/packages/{id}/unpublish/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_unpublish_package", requirements={"id"="\d+"}) |
|
141
|
|
|
* @Method("POST") |
|
142
|
|
|
*/ |
|
143
|
|
|
public function unpublishAction(Request $request, int $id) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
146
|
|
|
$package = $this->findOr404($id); |
|
147
|
|
|
$form = $this->createForm(UnpublishFromTenantsType::class, null, ['method' => $request->getMethod()]); |
|
148
|
|
|
|
|
149
|
|
|
$form->handleRequest($request); |
|
150
|
|
|
if ($form->isValid()) { |
|
151
|
|
|
$formData = $form->getData(); |
|
152
|
|
|
/** @var Collection $tenants */ |
|
153
|
|
|
$tenants = $formData['tenants']; |
|
154
|
|
|
$this->get('swp_core.article.publisher')->unpublish($package, $tenants->toArray()); |
|
155
|
|
|
|
|
156
|
|
|
return new SingleResourceResponse(null, new ResponseContext(200)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Update package. |
|
164
|
|
|
* |
|
165
|
|
|
* @param Request $request |
|
166
|
|
|
* @param int $id |
|
167
|
|
|
* |
|
168
|
|
|
* @ApiDoc( |
|
169
|
|
|
* resource=true, |
|
170
|
|
|
* description="Updates package", |
|
171
|
|
|
* statusCodes={ |
|
172
|
|
|
* 200="Returned on success.", |
|
173
|
|
|
* 400="Returned when validation failed.", |
|
174
|
|
|
* 500="Returned when unexpected error." |
|
175
|
|
|
* }, |
|
176
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\PackageType" |
|
177
|
|
|
* ) |
|
178
|
|
|
* |
|
179
|
|
|
* @Route("/api/{version}/packages/{id}/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_package", requirements={"id"="\d+"}) |
|
180
|
|
|
* |
|
181
|
|
|
* @Method("PATCH") |
|
182
|
|
|
* |
|
183
|
|
|
* @return SingleResourceResponse |
|
184
|
|
|
*/ |
|
185
|
|
|
public function updateAction(Request $request, int $id) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
188
|
|
|
$package = $this->findOr404($id); |
|
189
|
|
|
$form = $this->createForm(PackageType::class, $package, ['method' => $request->getMethod()]); |
|
190
|
|
|
|
|
191
|
|
|
$form->handleRequest($request); |
|
192
|
|
|
if ($form->isValid()) { |
|
193
|
|
|
if (ContentInterface::STATUS_CANCELED === $package->getPubStatus()) { |
|
194
|
|
|
$package->setStatus(ContentInterface::STATUS_CANCELED); |
|
195
|
|
|
} |
|
196
|
|
|
$this->getPackageRepository()->flush(); |
|
197
|
|
|
|
|
198
|
|
|
return new SingleResourceResponse($package, new ResponseContext(200)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param int $id |
|
206
|
|
|
* |
|
207
|
|
|
* @return null|object |
|
208
|
|
|
*/ |
|
209
|
|
View Code Duplication |
private function findOr404(int $id) |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
212
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
213
|
|
|
|
|
214
|
|
|
if (null === $package = $this->getPackageRepository()->findOneBy(['id' => $id, 'organization' => $tenantContext->getTenant()->getOrganization()])) { |
|
215
|
|
|
throw new NotFoundHttpException('Package was not found.'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $package; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
protected function getPackageRepository() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->get('swp.repository.package'); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.