1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. 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 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\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\Method; |
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
22
|
|
|
use SWP\Bundle\MenuBundle\Form\Type\MenuType; |
23
|
|
|
use SWP\Bundle\MenuBundle\Model\MenuItemInterface; |
24
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
25
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
28
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
29
|
|
|
|
30
|
|
|
class MenuController extends FOSRestController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Lists all registered menus. |
34
|
|
|
* |
35
|
|
|
* @ApiDoc( |
36
|
|
|
* resource=true, |
37
|
|
|
* description="Lists all registered menus", |
38
|
|
|
* statusCodes={ |
39
|
|
|
* 200="Returned on success.", |
40
|
|
|
* 404="No menus found." |
41
|
|
|
* } |
42
|
|
|
* ) |
43
|
|
|
* @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_menu") |
44
|
|
|
* @Method("GET") |
45
|
|
|
*/ |
46
|
2 |
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
47
|
|
|
{ |
48
|
2 |
|
$menuRepository = $this->get('swp.repository.menu'); |
49
|
|
|
|
50
|
2 |
|
$menus = $menuRepository->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request)); |
51
|
|
|
|
52
|
2 |
|
return $this->handleView(View::create($this->get('swp_pagination_rep')->createRepresentation($menus, $request), 200)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get single menu. |
57
|
|
|
* |
58
|
|
|
* @ApiDoc( |
59
|
|
|
* resource=true, |
60
|
|
|
* description="Get single menu", |
61
|
|
|
* statusCodes={ |
62
|
|
|
* 200="Returned on success.", |
63
|
|
|
* 404="Menu not found", |
64
|
|
|
* 422="Menu id is not number" |
65
|
|
|
* } |
66
|
|
|
* ) |
67
|
|
|
* @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_get_menu") |
68
|
|
|
* @Method("GET") |
69
|
|
|
*/ |
70
|
2 |
|
public function getAction($id) |
71
|
|
|
{ |
72
|
2 |
|
return $this->handleView(View::create($this->findOr404($id), 200)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create new menu. |
77
|
|
|
* |
78
|
|
|
* @ApiDoc( |
79
|
|
|
* resource=true, |
80
|
|
|
* description="Create new menu", |
81
|
|
|
* statusCodes={ |
82
|
|
|
* 201="Returned on success.", |
83
|
|
|
* 400="Returned when form have errors" |
84
|
|
|
* }, |
85
|
|
|
* input="SWP\Bundle\MenuBundle\Form\Type\MenuType" |
86
|
|
|
* ) |
87
|
|
|
* @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_create_menu") |
88
|
|
|
* @Method("POST") |
89
|
|
|
*/ |
90
|
4 |
|
public function createAction(Request $request) |
91
|
|
|
{ |
92
|
4 |
|
$route = null; |
93
|
4 |
|
if (array_key_exists('route', $request->request->get('menu'))) { |
94
|
|
|
$route = $this->get('swp.repository.route')->findOneBy(['id' => $request->request->get('menu')['route']]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* @var MenuItemInterface $route */ |
98
|
4 |
|
$menu = $this->get('swp.factory.menu')->createItem('', ['route' => $route ? $route->getName() : null]); |
99
|
4 |
|
$form = $this->createForm(MenuType::class, $menu, ['method' => $request->getMethod()]); |
100
|
|
|
|
101
|
4 |
|
$form->handleRequest($request); |
102
|
4 |
|
$this->ensureMenuItemExists($menu->getName()); |
103
|
|
|
|
104
|
4 |
|
if ($form->isValid()) { |
105
|
3 |
|
$this->get('swp.repository.menu')->add($menu); |
106
|
|
|
|
107
|
3 |
|
return $this->handleView(View::create($menu, 201)); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->handleView(View::create($form, 400)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Delete single menu. |
115
|
|
|
* |
116
|
|
|
* @ApiDoc( |
117
|
|
|
* resource=true, |
118
|
|
|
* description="Delete single menu", |
119
|
|
|
* statusCodes={ |
120
|
|
|
* 204="Returned on success.", |
121
|
|
|
* 404="Menu not found", |
122
|
|
|
* 422="Menu id is not number" |
123
|
|
|
* } |
124
|
|
|
* ) |
125
|
|
|
* @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_delete_menu") |
126
|
|
|
* @Method("DELETE") |
127
|
|
|
*/ |
128
|
1 |
|
public function deleteAction($id) |
129
|
|
|
{ |
130
|
1 |
|
$repository = $this->get('swp.repository.menu'); |
131
|
1 |
|
$repository->remove($this->findOr404($id)); |
132
|
|
|
|
133
|
1 |
|
return $this->handleView(View::create(true, 204)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Update single menu. |
138
|
|
|
* |
139
|
|
|
* @ApiDoc( |
140
|
|
|
* resource=true, |
141
|
|
|
* description="Update single menu", |
142
|
|
|
* statusCodes={ |
143
|
|
|
* 201="Returned on success.", |
144
|
|
|
* 404="Menu not found", |
145
|
|
|
* 422="Menu id is not number", |
146
|
|
|
* 405="Method Not Allowed" |
147
|
|
|
* }, |
148
|
|
|
* input="SWP\Bundle\TemplateEngineBundle\Form\Type\MenuType" |
149
|
|
|
* ) |
150
|
|
|
* @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_menu") |
151
|
|
|
* @Method("PATCH") |
152
|
|
|
*/ |
153
|
2 |
View Code Duplication |
public function updateAction(Request $request, $id) |
|
|
|
|
154
|
|
|
{ |
155
|
2 |
|
$objectManager = $this->get('swp.object_manager.menu'); |
156
|
2 |
|
$menu = $this->findOr404($id); |
157
|
|
|
|
158
|
2 |
|
$form = $this->createForm(MenuType::class, $menu, ['method' => $request->getMethod()]); |
159
|
2 |
|
$form->handleRequest($request); |
160
|
|
|
|
161
|
2 |
|
if ($form->isValid()) { |
162
|
2 |
|
$objectManager->flush(); |
163
|
|
|
|
164
|
2 |
|
return $this->handleView(View::create($menu, 200)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->handleView(View::create($form, 400)); |
168
|
|
|
} |
169
|
|
|
|
170
|
4 |
|
private function findOr404($id) |
171
|
|
|
{ |
172
|
4 |
|
if (null === $menu = $this->get('swp.repository.menu')->findOneBy(['id' => $id])) { |
173
|
1 |
|
throw new NotFoundHttpException('Menu was not found.'); |
174
|
|
|
} |
175
|
|
|
|
176
|
4 |
|
return $menu; |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
private function ensureMenuItemExists($name) |
180
|
|
|
{ |
181
|
4 |
|
if (null !== $this->get('swp.repository.menu')->findOneByName($name)) { |
182
|
|
|
throw new ConflictHttpException(sprintf('Menu item "%s" already exists!', $name)); |
183
|
|
|
} |
184
|
4 |
|
} |
185
|
|
|
} |
186
|
|
|
|
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.