|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine 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
|
|
|
namespace SWP\Bundle\TemplateEngineBundle\Controller; |
|
15
|
|
|
|
|
16
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
|
17
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
20
|
|
|
use FOS\RestBundle\View\View; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
|
23
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
24
|
|
|
use SWP\Bundle\TemplateEngineBundle\Model\Widget; |
|
25
|
|
|
use SWP\Bundle\TemplateEngineBundle\Form\Type\WidgetType; |
|
26
|
|
|
|
|
27
|
|
|
class WidgetController extends FOSRestController |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Lists all registered widgets. |
|
31
|
|
|
* |
|
32
|
|
|
* @ApiDoc( |
|
33
|
|
|
* resource=true, |
|
34
|
|
|
* description="Lists all registered widgets", |
|
35
|
|
|
* statusCodes={ |
|
36
|
|
|
* 200="Returned on success." |
|
37
|
|
|
* } |
|
38
|
|
|
* ) |
|
39
|
|
|
* @Route("/api/{version}/templates/widgtes/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_list_widgets") |
|
40
|
|
|
* @Method("GET") |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
|
45
|
|
|
$paginator = $this->get('knp_paginator'); |
|
46
|
|
|
$widgets = $paginator->paginate($entityManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Widget')->getAll()); |
|
47
|
|
|
|
|
48
|
|
|
if (count($widgets) == 0) { |
|
49
|
|
|
throw new NotFoundHttpException('Widgets were not found.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->handleView(View::create($this->container->get('swp_pagination_rep')->createRepresentation($widgets, $request), 200)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get single widget. |
|
57
|
|
|
* |
|
58
|
|
|
* @ApiDoc( |
|
59
|
|
|
* resource=true, |
|
60
|
|
|
* description="Get single widget", |
|
61
|
|
|
* statusCodes={ |
|
62
|
|
|
* 200="Returned on success.", |
|
63
|
|
|
* 404="Widget not found", |
|
64
|
|
|
* 422="Widget id is not number" |
|
65
|
|
|
* } |
|
66
|
|
|
* ) |
|
67
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_get_widget") |
|
68
|
|
|
* @Method("GET") |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getAction(Request $request, $id) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$id) { |
|
73
|
|
|
throw new UnprocessableEntityHttpException('You need to provide widget Id (integer).'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
|
77
|
|
|
$widget = $entityManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Widget')->getById($id)->getOneOrNullResult(); |
|
78
|
|
|
|
|
79
|
|
|
if (!$widget) { |
|
80
|
|
|
throw new NotFoundHttpException('Widget with this id was not found.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// return clean object for LINK requests |
|
84
|
|
|
if ($request->attributes->get('_link_request', false) === true) { |
|
85
|
|
|
return $widget; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->handleView(View::create($widget, 200)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Create new widget. |
|
93
|
|
|
* |
|
94
|
|
|
* @ApiDoc( |
|
95
|
|
|
* resource=true, |
|
96
|
|
|
* description="Create new widget", |
|
97
|
|
|
* statusCodes={ |
|
98
|
|
|
* 201="Returned on success.", |
|
99
|
|
|
* 400="Returned when form have errors" |
|
100
|
|
|
* }, |
|
101
|
|
|
* input="SWP\Bundle\TemplateEngineBundle\Form\Type\WidgetType" |
|
102
|
|
|
* ) |
|
103
|
|
|
* @Route("/api/{version}/templates/widgets", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_create_widget") |
|
104
|
|
|
* @Method("POST") |
|
105
|
|
|
*/ |
|
106
|
|
|
public function createAction(Request $request) |
|
107
|
|
|
{ |
|
108
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
|
109
|
|
|
|
|
110
|
|
|
$widget = new Widget(); |
|
111
|
|
|
$form = $this->createForm(WidgetType::class, $widget); |
|
112
|
|
|
$form->handleRequest($request); |
|
113
|
|
|
if ($form->isValid()) { |
|
114
|
|
|
$entityManager->persist($widget); |
|
115
|
|
|
$entityManager->flush(); |
|
116
|
|
|
|
|
117
|
|
|
return $this->handleView(View::create($widget, 201)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->handleView(View::create($form, 400)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Delete single widget. |
|
125
|
|
|
* |
|
126
|
|
|
* @ApiDoc( |
|
127
|
|
|
* resource=true, |
|
128
|
|
|
* description="Delete single widget", |
|
129
|
|
|
* statusCodes={ |
|
130
|
|
|
* 204="Returned on success.", |
|
131
|
|
|
* 404="Widget not found", |
|
132
|
|
|
* 422="Widget id is not number" |
|
133
|
|
|
* } |
|
134
|
|
|
* ) |
|
135
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_delete_widget") |
|
136
|
|
|
* @Method("DELETE") |
|
137
|
|
|
*/ |
|
138
|
|
|
public function deleteAction(Request $request, $id) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
if (!$id) { |
|
141
|
|
|
throw new UnprocessableEntityHttpException('You need to provide widget Id (integer).'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
|
145
|
|
|
$widget = $entityManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Widget')->getById($id)->getOneOrNullResult(); |
|
146
|
|
|
|
|
147
|
|
|
if (!$widget) { |
|
148
|
|
|
throw new NotFoundHttpException('Widget with this id was not found.'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
foreach ($widget->getContainers() as $containerWidget) { |
|
152
|
|
|
$entityManager->remove($containerWidget); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$entityManager->remove($widget); |
|
156
|
|
|
$entityManager->flush(); |
|
157
|
|
|
|
|
158
|
|
|
return $this->handleView(View::create(null, 204)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Update single widget. |
|
163
|
|
|
* |
|
164
|
|
|
* @ApiDoc( |
|
165
|
|
|
* resource=true, |
|
166
|
|
|
* description="Update single widget", |
|
167
|
|
|
* statusCodes={ |
|
168
|
|
|
* 201="Returned on success.", |
|
169
|
|
|
* 404="Widget not found", |
|
170
|
|
|
* 422="Widget id is not number", |
|
171
|
|
|
* 405="Method Not Allowed" |
|
172
|
|
|
* }, |
|
173
|
|
|
* input="SWP\Bundle\TemplateEngineBundle\Form\Type\WidgetType" |
|
174
|
|
|
* ) |
|
175
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_update_widget") |
|
176
|
|
|
* @Method("PATCH") |
|
177
|
|
|
*/ |
|
178
|
|
|
public function updateAction(Request $request, $id) |
|
179
|
|
|
{ |
|
180
|
|
|
if (!$id) { |
|
181
|
|
|
throw new UnprocessableEntityHttpException('You need to provide container Id (integer).'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
|
185
|
|
|
$widget = $entityManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Widget')->getById($id)->getOneOrNullResult(); |
|
186
|
|
|
|
|
187
|
|
|
if (!$widget) { |
|
188
|
|
|
throw new NotFoundHttpException('Widget with this id was not found.'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$form = $this->createForm(WidgetType::class, $widget, array( |
|
192
|
|
|
'method' => $request->getMethod(), |
|
193
|
|
|
)); |
|
194
|
|
|
|
|
195
|
|
|
$form->handleRequest($request); |
|
196
|
|
|
if ($form->isValid()) { |
|
197
|
|
|
$entityManager->flush($widget); |
|
198
|
|
|
$entityManager->refresh($widget); |
|
199
|
|
|
|
|
200
|
|
|
return $this->handleView(View::create($widget, 201)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $this->handleView(View::create($form, 200)); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
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.