1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Controller; |
16
|
|
|
|
17
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
18
|
|
|
use FOS\RestBundle\View\View; |
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\ContentBundle\Model\RouteInterface; |
24
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
25
|
|
|
use SWP\Bundle\ContentBundle\Form\Type\RouteType; |
26
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
27
|
|
|
use SWP\Component\Common\Event\HttpCacheEvent; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
31
|
|
|
|
32
|
|
|
class RouteController extends FOSRestController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Lists current tenant routes. |
36
|
|
|
* |
37
|
|
|
* @ApiDoc( |
38
|
|
|
* resource=true, |
39
|
|
|
* description="Lists current tenant routes", |
40
|
|
|
* statusCodes={ |
41
|
|
|
* 200="Returned on success." |
42
|
|
|
* } |
43
|
|
|
* ) |
44
|
|
|
* @Route("/api/{version}/content/routes/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_routes") |
45
|
|
|
* @Method("GET") |
46
|
|
|
* |
47
|
|
|
* @Cache(expires="10 minutes", public=true) |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$routeRepository = $this->get('swp.repository.route'); |
52
|
|
|
|
53
|
|
|
$routes = $routeRepository->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request)); |
54
|
|
|
|
55
|
|
|
return $this->handleView(View::create($this->get('swp_pagination_rep')->createRepresentation($routes, $request), 200)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Show single tenant route. |
60
|
|
|
* |
61
|
|
|
* @ApiDoc( |
62
|
|
|
* resource=true, |
63
|
|
|
* description="Show single tenant route", |
64
|
|
|
* statusCodes={ |
65
|
|
|
* 200="Returned on success." |
66
|
|
|
* } |
67
|
|
|
* ) |
68
|
|
|
* @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_routes", requirements={"id"=".+"}) |
69
|
|
|
* @Method("GET") |
70
|
|
|
* |
71
|
|
|
* @Cache(expires="10 minutes", public=true) |
72
|
|
|
*/ |
73
|
1 |
|
public function getAction($id) |
74
|
|
|
{ |
75
|
1 |
|
return $this->handleView(View::create($this->findOr404($id), 200)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Delete single tenant route. |
80
|
|
|
* |
81
|
|
|
* @ApiDoc( |
82
|
|
|
* resource=true, |
83
|
|
|
* description="Delete single tenant route", |
84
|
|
|
* statusCodes={ |
85
|
|
|
* 204="Returned on success." |
86
|
|
|
* } |
87
|
|
|
* ) |
88
|
|
|
* @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_delete_routes", requirements={"id"=".+"}) |
89
|
|
|
* @Method("DELETE") |
90
|
|
|
*/ |
91
|
1 |
|
public function deleteAction($id) |
92
|
|
|
{ |
93
|
1 |
|
$repository = $this->get('swp.repository.route'); |
94
|
1 |
|
$route = $this->findOr404($id); |
95
|
1 |
|
$this->get('event_dispatcher') |
96
|
1 |
|
->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); |
97
|
|
|
|
98
|
1 |
|
if (null !== $route->getContent()) { |
99
|
1 |
|
throw new ConflictHttpException('Route has content attached to it.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$repository->remove($route); |
103
|
|
|
|
104
|
1 |
|
return $this->handleView(View::create(true, 204)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates routes for current tenant. |
109
|
|
|
* |
110
|
|
|
* Parameter `type` cane have one of two values: `content` or `collection`. |
111
|
|
|
* |
112
|
|
|
* @ApiDoc( |
113
|
|
|
* resource=true, |
114
|
|
|
* description="Create new route", |
115
|
|
|
* statusCodes={ |
116
|
|
|
* 201="Returned on success.", |
117
|
|
|
* 400="Returned when not valid data." |
118
|
|
|
* }, |
119
|
|
|
* input="SWP\Bundle\ContentBundle\Form\Type\RouteType" |
120
|
|
|
* ) |
121
|
|
|
* @Route("/api/{version}/content/routes/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_create_routes") |
122
|
|
|
* @Method("POST") |
123
|
|
|
*/ |
124
|
24 |
View Code Duplication |
public function createAction(Request $request) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
/** @var RouteInterface $route */ |
127
|
24 |
|
$route = $this->get('swp.factory.route')->create(); |
128
|
24 |
|
$form = $this->createForm(RouteType::class, $route, ['method' => $request->getMethod()]); |
129
|
|
|
|
130
|
24 |
|
$form->handleRequest($request); |
131
|
24 |
|
$this->ensureRouteExists($route->getName()); |
132
|
|
|
|
133
|
24 |
|
if ($form->isValid()) { |
134
|
22 |
|
$route = $this->get('swp.service.route')->createRoute($form->getData()); |
135
|
|
|
|
136
|
22 |
|
$this->get('swp.repository.route')->add($route); |
137
|
22 |
|
$this->get('event_dispatcher') |
138
|
22 |
|
->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); |
139
|
|
|
|
140
|
22 |
|
return $this->handleView(View::create($route, 201)); |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
return $this->handleView(View::create($form, 400)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Updates routes for current tenant. |
148
|
|
|
* |
149
|
|
|
* Parameter `type` cane have one of two values: `content` or `collection`. |
150
|
|
|
* |
151
|
|
|
* @ApiDoc( |
152
|
|
|
* resource=true, |
153
|
|
|
* description="Update single route", |
154
|
|
|
* statusCodes={ |
155
|
|
|
* 200="Returned on success.", |
156
|
|
|
* 400="Returned when not valid data.", |
157
|
|
|
* 404="Returned when not found.", |
158
|
|
|
* 409="Returned on conflict." |
159
|
|
|
* }, |
160
|
|
|
* input="SWP\Bundle\ContentBundle\Form\Type\RouteType" |
161
|
|
|
* ) |
162
|
|
|
* @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_routes", requirements={"id"=".+"}) |
163
|
|
|
* @Method("PATCH") |
164
|
|
|
*/ |
165
|
5 |
View Code Duplication |
public function updateAction(Request $request, $id) |
|
|
|
|
166
|
|
|
{ |
167
|
5 |
|
$objectManager = $this->get('swp.object_manager.route'); |
168
|
5 |
|
$route = $this->findOr404($id); |
169
|
5 |
|
$form = $this->createForm(RouteType::class, $route, ['method' => $request->getMethod()]); |
170
|
5 |
|
$form->handleRequest($request); |
171
|
|
|
|
172
|
5 |
|
if ($form->isValid()) { |
173
|
5 |
|
$route = $this->get('swp.service.route')->updateRoute($form->getData()); |
174
|
|
|
|
175
|
5 |
|
$objectManager->flush(); |
176
|
5 |
|
$this->get('event_dispatcher') |
177
|
5 |
|
->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); |
178
|
|
|
|
179
|
5 |
|
return $this->handleView(View::create($route, 200)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->handleView(View::create($form, 400)); |
183
|
|
|
} |
184
|
|
|
|
185
|
6 |
|
private function findOr404($id) |
186
|
|
|
{ |
187
|
6 |
|
if (null === $route = $this->get('swp.provider.route')->getOneById($id)) { |
188
|
|
|
throw new NotFoundHttpException('Route was not found.'); |
189
|
|
|
} |
190
|
|
|
|
191
|
6 |
|
return $route; |
192
|
|
|
} |
193
|
|
|
|
194
|
24 |
|
private function ensureRouteExists($name) |
195
|
|
|
{ |
196
|
24 |
|
if (null !== $this->get('swp.repository.route')->findOneByName($name)) { |
197
|
|
|
throw new ConflictHttpException(sprintf('Route "%s" already exists!', $name)); |
198
|
|
|
} |
199
|
24 |
|
} |
200
|
|
|
} |
201
|
|
|
|
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.