|
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 Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
23
|
|
|
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents; |
|
24
|
|
|
use SWP\Bundle\RuleBundle\Form\Type\RuleType; |
|
25
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
26
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
|
27
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
|
28
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
|
29
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
|
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
31
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
32
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
33
|
|
|
|
|
34
|
|
|
class OrganizationRuleController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* List all current organization's rules. |
|
38
|
|
|
* |
|
39
|
|
|
* @ApiDoc( |
|
40
|
|
|
* resource=true, |
|
41
|
|
|
* description="List all current organization's articles", |
|
42
|
|
|
* statusCodes={ |
|
43
|
|
|
* 200="Returned on success.", |
|
44
|
|
|
* 500="Returned when unexpected error occurred." |
|
45
|
|
|
* }, |
|
46
|
|
|
* filters={ |
|
47
|
|
|
* {"name"="sorting", "dataType"="string", "pattern"="[updatedAt]=asc|desc"} |
|
48
|
|
|
* } |
|
49
|
|
|
* ) |
|
50
|
|
|
* @Route("/api/{version}/organization/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_organization_rules") |
|
51
|
|
|
* @Method("GET") |
|
52
|
|
|
* |
|
53
|
|
|
* @Cache(expires="10 minutes", public=true) |
|
54
|
|
|
*/ |
|
55
|
|
|
public function rulesAction(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
58
|
|
|
|
|
59
|
|
|
$this->getEventDispatcher()->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
60
|
|
|
|
|
61
|
|
|
$repository = $this->getRuleRepository(); |
|
62
|
|
|
$rules = $repository->getPaginatedByCriteria( |
|
63
|
|
|
new Criteria([ |
|
64
|
|
|
'organization' => $tenantContext->getTenant()->getOrganization(), |
|
65
|
|
|
'tenantCode' => null, |
|
66
|
|
|
]), |
|
67
|
|
|
$request->query->get('sorting', []), |
|
68
|
|
|
new PaginationData($request) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return new ResourcesListResponse($rules); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create a new Organization Rule. |
|
76
|
|
|
* |
|
77
|
|
|
* @ApiDoc( |
|
78
|
|
|
* resource=true, |
|
79
|
|
|
* description="Create a new organization rule", |
|
80
|
|
|
* statusCodes={ |
|
81
|
|
|
* 201="Returned on success.", |
|
82
|
|
|
* 400="Returned on validation error.", |
|
83
|
|
|
* 405="Method Not Allowed." |
|
84
|
|
|
* }, |
|
85
|
|
|
* input="SWP\Bundle\RuleBundle\Form\Type\RuleType" |
|
86
|
|
|
* ) |
|
87
|
|
|
* @Route("/api/{version}/organization/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_create_organization_rule") |
|
88
|
|
|
* @Method("POST") |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function createAction(Request $request) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$ruleRepository = $this->getRuleRepository(); |
|
93
|
|
|
|
|
94
|
|
|
$rule = $this->get('swp.factory.rule')->create(); |
|
95
|
|
|
$form = $this->createForm(RuleType::class, $rule); |
|
96
|
|
|
$form->handleRequest($request); |
|
97
|
|
|
|
|
98
|
|
|
if ($form->isValid()) { |
|
99
|
|
|
$ruleRepository->add($rule); |
|
100
|
|
|
$rule->setTenantCode(null); |
|
101
|
|
|
$ruleRepository->flush(); |
|
102
|
|
|
|
|
103
|
|
|
return new SingleResourceResponse($rule, new ResponseContext(201)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Show single organiation's rule. |
|
111
|
|
|
* |
|
112
|
|
|
* @ApiDoc( |
|
113
|
|
|
* resource=true, |
|
114
|
|
|
* description="Show single organization rule", |
|
115
|
|
|
* statusCodes={ |
|
116
|
|
|
* 200="Returned on success." |
|
117
|
|
|
* } |
|
118
|
|
|
* ) |
|
119
|
|
|
* @Route("/api/{version}/organization/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_show_organization_rule", requirements={"id"="\d+"}) |
|
120
|
|
|
* @Method("GET") |
|
121
|
|
|
* |
|
122
|
|
|
* @Cache(expires="10 minutes", public=true) |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getAction(int $id) |
|
125
|
|
|
{ |
|
126
|
|
|
return new SingleResourceResponse($this->findOr404($id)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Updates organization's rule. |
|
131
|
|
|
* |
|
132
|
|
|
* @ApiDoc( |
|
133
|
|
|
* resource=true, |
|
134
|
|
|
* description="Updates organization rule", |
|
135
|
|
|
* statusCodes={ |
|
136
|
|
|
* 200="Returned on success.", |
|
137
|
|
|
* 400="Returned when validation failed.", |
|
138
|
|
|
* 500="Returned when unexpected error." |
|
139
|
|
|
* }, |
|
140
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\RuleType" |
|
141
|
|
|
* ) |
|
142
|
|
|
* @Route("/api/{version}/organization/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_organization_rule", requirements={"id"="\d+"}) |
|
143
|
|
|
* @Method("PATCH") |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function updateRuleAction(Request $request, int $id) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$objectManager = $this->get('swp.object_manager.rule'); |
|
148
|
|
|
$rule = $this->findOr404($id); |
|
149
|
|
|
$form = $this->createForm(RuleType::class, $rule, ['method' => $request->getMethod()]); |
|
150
|
|
|
|
|
151
|
|
|
$form->handleRequest($request); |
|
152
|
|
|
if ($form->isValid()) { |
|
153
|
|
|
$objectManager->flush(); |
|
154
|
|
|
$objectManager->refresh($rule); |
|
155
|
|
|
|
|
156
|
|
|
return new SingleResourceResponse($rule); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return new SingleResourceResponse($form, new ResponseContext(500)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Delete single organization rule. |
|
164
|
|
|
* |
|
165
|
|
|
* @ApiDoc( |
|
166
|
|
|
* resource=true, |
|
167
|
|
|
* description="Delete single organization rule", |
|
168
|
|
|
* statusCodes={ |
|
169
|
|
|
* 204="Returned on success.", |
|
170
|
|
|
* 404="Returned when rule not found.", |
|
171
|
|
|
* 405="Returned when method not allowed." |
|
172
|
|
|
* } |
|
173
|
|
|
* ) |
|
174
|
|
|
* @Route("/api/{version}/organization/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_delete_organization_rule", requirements={"id"="\d+"}) |
|
175
|
|
|
* @Method("DELETE") |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function deleteAction(int $id) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$rule = $this->findOr404($id); |
|
180
|
|
|
$ruleRepository = $this->get('swp.repository.rule'); |
|
181
|
|
|
$ruleRepository->remove($rule); |
|
182
|
|
|
|
|
183
|
|
|
return new SingleResourceResponse(null, new ResponseContext(204)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function findOr404(int $id) |
|
187
|
|
|
{ |
|
188
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
189
|
|
|
$this->getEventDispatcher()->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
190
|
|
|
|
|
191
|
|
View Code Duplication |
if (null === ($rule = $this->getRuleRepository()->findOneBy([ |
|
|
|
|
|
|
192
|
|
|
'id' => $id, |
|
193
|
|
|
'organization' => $tenantContext->getTenant()->getOrganization(), |
|
194
|
|
|
'tenantCode' => null, |
|
195
|
|
|
]))) { |
|
196
|
|
|
throw new NotFoundHttpException('Organization rule was not found.'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $rule; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
private function getRuleRepository() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->get('swp.repository.rule'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function getEventDispatcher() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->get('event_dispatcher'); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
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.