1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core 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\CoreBundle\Controller; |
16
|
|
|
|
17
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
21
|
|
|
use SWP\Bundle\ContentListBundle\Form\Type\ContentListType; |
22
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
23
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
24
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
25
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
26
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
27
|
|
|
use SWP\Component\ContentList\Model\ContentListInterface; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
|
33
|
|
|
class ContentListController extends Controller |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @ApiDoc( |
37
|
|
|
* resource=true, |
38
|
|
|
* description="Lists all content lists", |
39
|
|
|
* statusCodes={ |
40
|
|
|
* 200="Returned on success." |
41
|
|
|
* } |
42
|
|
|
* ) |
43
|
|
|
* @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_lists") |
44
|
|
|
* @Method("GET") |
45
|
|
|
* |
46
|
|
|
* @Cache(expires="10 minutes", public=true) |
47
|
|
|
*/ |
48
|
1 |
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
49
|
|
|
{ |
50
|
1 |
|
$repository = $this->get('swp.repository.content_list'); |
51
|
|
|
|
52
|
1 |
|
$lists = $repository->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request)); |
53
|
|
|
|
54
|
1 |
|
return new ResourcesListResponse($lists); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ApiDoc( |
59
|
|
|
* resource=true, |
60
|
|
|
* description="Show single content list", |
61
|
|
|
* statusCodes={ |
62
|
|
|
* 200="Returned on success." |
63
|
|
|
* } |
64
|
|
|
* ) |
65
|
|
|
* @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_lists", requirements={"id"="\d+"}) |
66
|
|
|
* @Method("GET") |
67
|
|
|
* |
68
|
|
|
* @Cache(expires="10 minutes", public=true) |
69
|
|
|
*/ |
70
|
1 |
|
public function getAction($id) |
71
|
|
|
{ |
72
|
1 |
|
return new SingleResourceResponse($this->findOr404($id)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @ApiDoc( |
77
|
|
|
* resource=true, |
78
|
|
|
* description="Create new content list", |
79
|
|
|
* statusCodes={ |
80
|
|
|
* 201="Returned on success.", |
81
|
|
|
* 400="Returned when not valid data." |
82
|
|
|
* }, |
83
|
|
|
* input="SWP\Bundle\ContentListBundle\Form\Type\ContentListType" |
84
|
|
|
* ) |
85
|
|
|
* @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_create_lists") |
86
|
|
|
* @Method("POST") |
87
|
|
|
*/ |
88
|
6 |
View Code Duplication |
public function createAction(Request $request) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
/* @var ContentListInterface $contentList */ |
91
|
6 |
|
$contentList = $this->get('swp.factory.content_list')->create(); |
92
|
6 |
|
$form = $this->createForm(ContentListType::class, $contentList, ['method' => $request->getMethod()]); |
93
|
|
|
|
94
|
6 |
|
$form->handleRequest($request); |
95
|
6 |
|
$this->ensureContentListExists($contentList->getName()); |
96
|
|
|
|
97
|
6 |
|
if ($form->isValid()) { |
98
|
5 |
|
$this->get('swp.repository.content_list')->add($contentList); |
99
|
|
|
|
100
|
5 |
|
return new SingleResourceResponse($contentList, new ResponseContext(201)); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @ApiDoc( |
108
|
|
|
* resource=true, |
109
|
|
|
* description="Update single content list", |
110
|
|
|
* statusCodes={ |
111
|
|
|
* 200="Returned on success.", |
112
|
|
|
* 400="Returned when not valid data.", |
113
|
|
|
* 404="Returned when not found.", |
114
|
|
|
* 409="Returned on conflict." |
115
|
|
|
* }, |
116
|
|
|
* input="SWP\Bundle\ContentListBundle\Form\Type\ContentListType" |
117
|
|
|
* ) |
118
|
|
|
* @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_lists", requirements={"id"="\d+"}) |
119
|
|
|
* @Method("PATCH") |
120
|
|
|
*/ |
121
|
1 |
|
public function updateAction(Request $request, $id) |
122
|
|
|
{ |
123
|
1 |
|
$objectManager = $this->get('swp.object_manager.content_list'); |
124
|
1 |
|
$contentList = $this->findOr404($id); |
125
|
1 |
|
$form = $this->createForm(ContentListType::class, $contentList, ['method' => $request->getMethod()]); |
126
|
1 |
|
$form->handleRequest($request); |
127
|
|
|
|
128
|
1 |
|
if ($form->isValid()) { |
129
|
1 |
|
$objectManager->flush(); |
130
|
|
|
|
131
|
1 |
|
return new SingleResourceResponse($contentList); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @ApiDoc( |
139
|
|
|
* resource=true, |
140
|
|
|
* description="Delete single content list", |
141
|
|
|
* statusCodes={ |
142
|
|
|
* 204="Returned on success." |
143
|
|
|
* } |
144
|
|
|
* ) |
145
|
|
|
* @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"}) |
146
|
|
|
* @Method("DELETE") |
147
|
|
|
*/ |
148
|
2 |
|
public function deleteAction($id) |
149
|
|
|
{ |
150
|
2 |
|
$repository = $this->get('swp.repository.content_list'); |
151
|
2 |
|
$contentList = $this->findOr404($id); |
152
|
|
|
|
153
|
1 |
|
$repository->remove($contentList); |
154
|
|
|
|
155
|
1 |
|
return new SingleResourceResponse(null, new ResponseContext(204)); |
156
|
|
|
} |
157
|
|
|
|
158
|
4 |
|
private function findOr404($id) |
159
|
|
|
{ |
160
|
4 |
|
if (null === $list = $this->get('swp.repository.content_list')->findOneById($id)) { |
161
|
1 |
|
throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id)); |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
return $list; |
165
|
|
|
} |
166
|
|
|
|
167
|
6 |
|
private function ensureContentListExists($name) |
168
|
|
|
{ |
169
|
6 |
|
if (null !== $this->get('swp.repository.content_list')->findOneByName($name)) { |
170
|
|
|
throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name)); |
171
|
|
|
} |
172
|
6 |
|
} |
173
|
|
|
} |
174
|
|
|
|
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.