Skip to content

Commit 6645d4b

Browse files
committed
Refactor EventController to not use FOS RestBundle and JMS Serializer
1 parent d02dd2b commit 6645d4b

File tree

7 files changed

+150
-162
lines changed

7 files changed

+150
-162
lines changed

config/packages/sulu_admin.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ sulu_admin:
1111
resources:
1212
events:
1313
routes:
14-
list: app.get_events
14+
list: app.get_event_list
1515
detail: app.get_event
1616

1717
# Registering Selection Field Types in this section

config/routes_admin.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
# Define your admin routes here
2-
app.event_api:
3-
type: rest
4-
resource: App\Controller\Admin\EventController
5-
prefix: /admin/api
6-
name_prefix: app.

src/Controller/Admin/EventController.php

+91-79
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,19 @@
77
use App\Common\DoctrineListRepresentationFactory;
88
use App\Entity\Event;
99
use App\Repository\EventRepository;
10-
use FOS\RestBundle\Controller\Annotations as Rest;
11-
use FOS\RestBundle\Controller\Annotations\RouteResource;
12-
use FOS\RestBundle\Routing\ClassResourceInterface;
13-
use FOS\RestBundle\View\ViewHandlerInterface;
1410
use Sulu\Bundle\MediaBundle\Entity\MediaRepositoryInterface;
15-
use Sulu\Component\Rest\AbstractRestController;
11+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1612
use Symfony\Component\HttpFoundation\Request;
1713
use Symfony\Component\HttpFoundation\Response;
1814
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15+
use Symfony\Component\Routing\Annotation\Route;
2016

21-
/**
22-
* @RouteResource("event")
23-
*/
24-
class EventController extends AbstractRestController implements ClassResourceInterface
17+
class EventController extends AbstractController
2518
{
2619
/**
2720
* @var EventRepository
2821
*/
29-
private $repository;
22+
private $eventRepository;
3023

3124
/**
3225
* @var MediaRepositoryInterface
@@ -42,55 +35,60 @@ public function __construct(
4235
EventRepository $repository,
4336
MediaRepositoryInterface $mediaRepository,
4437
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
45-
ViewHandlerInterface $viewHandler,
46-
?TokenStorageInterface $tokenStorage = null
4738
) {
48-
$this->repository = $repository;
39+
$this->eventRepository = $repository;
4940
$this->mediaRepository = $mediaRepository;
5041
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
51-
52-
parent::__construct($viewHandler, $tokenStorage);
5342
}
5443

55-
public function cgetAction(Request $request): Response
44+
/**
45+
* @Route("/admin/api/events/{id}", methods={"GET"}, name="app.get_event")
46+
*/
47+
public function getAction(int $id, Request $request): Response
5648
{
57-
$locale = $request->query->get('locale');
58-
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
59-
Event::RESOURCE_KEY,
60-
[],
61-
['locale' => $locale]
62-
);
49+
$event = $this->load($id, $request);
50+
if (!$event) {
51+
throw new NotFoundHttpException();
52+
}
6353

64-
return $this->handleView($this->view($listRepresentation));
54+
return $this->json($this->getDataForEntity($event));
6555
}
6656

67-
public function getAction(int $id, Request $request): Response
57+
/**
58+
* @Route("/admin/api/events/{id}", methods={"PUT"}, name="app.put_event")
59+
*/
60+
public function putAction(int $id, Request $request): Response
6861
{
69-
$entity = $this->load($id, $request);
70-
if (!$entity) {
62+
$event = $this->load($id, $request);
63+
if (!$event) {
7164
throw new NotFoundHttpException();
7265
}
7366

74-
return $this->handleView($this->view($entity));
67+
$this->mapDataToEntity($request->toArray(), $event);
68+
$this->save($event);
69+
70+
return $this->json($this->getDataForEntity($event));
7571
}
7672

73+
/**
74+
* @Route("/admin/api/events", methods={"POST"}, name="app.post_event")
75+
*/
7776
public function postAction(Request $request): Response
7877
{
79-
$entity = $this->create($request);
80-
81-
$this->mapDataToEntity($request->request->all(), $entity);
78+
$event = $this->create($request);
8279

83-
$this->save($entity);
80+
$this->mapDataToEntity($request->toArray(), $event);
81+
$this->save($event);
8482

85-
return $this->handleView($this->view($entity));
83+
return $this->json($this->getDataForEntity($event), 201);
8684
}
8785

8886
/**
89-
* @Rest\Post("/events/{id}")
87+
* @Route("/admin/api/events/{id}", methods={"POST"}, name="app.post_event_trigger")
9088
*/
9189
public function postTriggerAction(int $id, Request $request): Response
9290
{
93-
$event = $this->repository->findById($id, (string) $this->getLocale($request));
91+
$event = $this->eventRepository->findById($id, (string) $this->getLocale($request));
9492
if (!$event) {
9593
throw new NotFoundHttpException();
9694
}
@@ -104,83 +102,97 @@ public function postTriggerAction(int $id, Request $request): Response
104102
break;
105103
}
106104

107-
$this->repository->save($event);
105+
$this->eventRepository->save($event);
108106

109-
return $this->handleView($this->view($event));
107+
return $this->json($this->getDataForEntity($event));
110108
}
111109

112-
public function putAction(int $id, Request $request): Response
110+
/**
111+
* @Route("/admin/api/events/{id}", methods={"DELETE"}, name="app.delete_event")
112+
*/
113+
public function deleteAction(int $id): Response
113114
{
114-
$entity = $this->load($id, $request);
115-
if (!$entity) {
116-
throw new NotFoundHttpException();
117-
}
115+
$this->remove($id);
118116

119-
$this->mapDataToEntity($request->request->all(), $entity);
117+
return $this->json(null, 204);
118+
}
120119

121-
$this->save($entity);
120+
/**
121+
* @Route("/admin/api/events", methods={"GET"}, name="app.get_event_list")
122+
*/
123+
public function getListAction(Request $request): Response
124+
{
125+
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
126+
Event::RESOURCE_KEY,
127+
[],
128+
['locale' => $this->getLocale($request)]
129+
);
122130

123-
return $this->handleView($this->view($entity));
131+
return $this->json($listRepresentation->toArray());
124132
}
125133

126-
public function deleteAction(int $id): Response
134+
/**
135+
* @return array<string, mixed>
136+
*/
137+
protected function getDataForEntity(Event $entity): array
127138
{
128-
$this->remove($id);
129-
130-
return $this->handleView($this->view());
139+
$image = $entity->getImage();
140+
$startDate = $entity->getStartDate();
141+
$endDate = $entity->getEndDate();
142+
143+
return [
144+
'id' => $entity->getId(),
145+
'enabled' => $entity->isEnabled(),
146+
'title' => $entity->getTitle(),
147+
'image' => $image
148+
? ['id' => $image->getId()]
149+
: null,
150+
'teaser' => $entity->getTeaser(),
151+
'description' => $entity->getDescription(),
152+
'startDate' => $startDate ? $startDate->format('c') : null,
153+
'endDate' => $endDate ? $endDate->format('c') : null,
154+
'location' => $entity->getLocation(),
155+
];
131156
}
132157

133158
/**
134159
* @param array<string, mixed> $data
135160
*/
136161
protected function mapDataToEntity(array $data, Event $entity): void
137162
{
138-
$entity->setTitle($data['title']);
139-
140-
$image = null;
141-
if ($imageId = ($data['image']['id'] ?? null)) {
142-
$image = $this->mediaRepository->findMediaById($imageId);
143-
}
144-
$entity->setImage($image);
145-
146-
if ($teaser = $data['teaser'] ?? null) {
147-
$entity->setTeaser($teaser);
148-
}
149-
150-
if ($description = $data['description'] ?? null) {
151-
$entity->setDescription($description);
152-
}
153-
154-
if ($startDate = $data['startDate'] ?? null) {
155-
$entity->setStartDate(new \DateTimeImmutable($startDate));
156-
}
163+
$imageId = $data['image']['id'] ?? null;
157164

158-
if ($endDate = $data['endDate'] ?? null) {
159-
$entity->setEndDate(new \DateTimeImmutable($endDate));
160-
}
161-
162-
if ($location = $data['location'] ?? null) {
163-
$entity->setLocation($location);
164-
}
165+
$entity->setTitle($data['title']);
166+
$entity->setImage($imageId ? $this->mediaRepository->findMediaById($imageId) : null);
167+
$entity->setTeaser($data['teaser'] ?? '');
168+
$entity->setDescription($data['description'] ?? '');
169+
$entity->setStartDate($data['startDate'] ? new \DateTimeImmutable($data['startDate']) : null);
170+
$entity->setEndDate($data['endDate'] ? new \DateTimeImmutable($data['endDate']) : null);
171+
$entity->setLocation($data['location'] ?? null);
165172
}
166173

167174
protected function load(int $id, Request $request): ?Event
168175
{
169-
return $this->repository->findById($id, (string) $this->getLocale($request));
176+
return $this->eventRepository->findById($id, (string) $this->getLocale($request));
170177
}
171178

172179
protected function create(Request $request): Event
173180
{
174-
return $this->repository->create((string) $this->getLocale($request));
181+
return $this->eventRepository->create((string) $this->getLocale($request));
175182
}
176183

177184
protected function save(Event $entity): void
178185
{
179-
$this->repository->save($entity);
186+
$this->eventRepository->save($entity);
180187
}
181188

182189
protected function remove(int $id): void
183190
{
184-
$this->repository->remove($id);
191+
$this->eventRepository->remove($id);
192+
}
193+
194+
public function getLocale(Request $request): ?string
195+
{
196+
return $request->query->has('locale') ? (string) $request->query->get('locale') : null;
185197
}
186198
}

src/Controller/Website/EventWebsiteController.php

+20-16
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@
1313

1414
class EventWebsiteController extends AbstractController
1515
{
16+
/**
17+
* @var EventRepository
18+
*/
19+
private $eventRepository;
20+
21+
/**
22+
* @var TemplateAttributeResolverInterface
23+
*/
24+
private $templateAttributeResolver;
25+
26+
public function __construct(
27+
EventRepository $repository,
28+
TemplateAttributeResolverInterface $templateAttributeResolver
29+
) {
30+
$this->eventRepository = $repository;
31+
$this->templateAttributeResolver = $templateAttributeResolver;
32+
}
33+
1634
public function indexAction(int $id, Request $request): Response
1735
{
18-
$event = $this->get(EventRepository::class)->findById($id, $request->getLocale());
36+
$event = $this->eventRepository->findById($id, $request->getLocale());
1937
if (!$event) {
2038
throw new NotFoundHttpException();
2139
}
2240

2341
return $this->render(
2442
'events/index.html.twig',
25-
$this->get(TemplateAttributeResolverInterface::class)->resolve(
43+
$this->templateAttributeResolver->resolve(
2644
[
2745
'event' => $event,
2846
'content' => ['title' => $event->getTitle()],
2947
]
3048
)
3149
);
3250
}
33-
34-
/**
35-
* @return mixed[]
36-
*/
37-
public static function getSubscribedServices(): array
38-
{
39-
return array_merge(
40-
parent::getSubscribedServices(),
41-
[
42-
EventRepository::class,
43-
TemplateAttributeResolverInterface::class,
44-
]
45-
);
46-
}
4751
}

0 commit comments

Comments
 (0)