Skip to content

Commit 7a92790

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

File tree

7 files changed

+123
-127
lines changed

7 files changed

+123
-127
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

+83-53
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,30 +102,57 @@ 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
/**
@@ -166,21 +191,26 @@ protected function mapDataToEntity(array $data, Event $entity): void
166191

167192
protected function load(int $id, Request $request): ?Event
168193
{
169-
return $this->repository->findById($id, (string) $this->getLocale($request));
194+
return $this->eventRepository->findById($id, (string) $this->getLocale($request));
170195
}
171196

172197
protected function create(Request $request): Event
173198
{
174-
return $this->repository->create((string) $this->getLocale($request));
199+
return $this->eventRepository->create((string) $this->getLocale($request));
175200
}
176201

177202
protected function save(Event $entity): void
178203
{
179-
$this->repository->save($entity);
204+
$this->eventRepository->save($entity);
180205
}
181206

182207
protected function remove(int $id): void
183208
{
184-
$this->repository->remove($id);
209+
$this->eventRepository->remove($id);
210+
}
211+
212+
public function getLocale(Request $request): ?string
213+
{
214+
return $request->query->has('locale') ? (string) $request->query->get('locale') : null;
185215
}
186216
}

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
}

src/Entity/Event.php

-31
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Common\Collections\Collection;
99
use Doctrine\ORM\Mapping as ORM;
10-
use JMS\Serializer\Annotation as Serializer;
1110
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
1211

1312
/**
@@ -58,8 +57,6 @@ class Event
5857
* @var Collection<string, EventTranslation>
5958
*
6059
* @ORM\OneToMany(targetEntity="App\Entity\EventTranslation", mappedBy="event", cascade={"ALL"}, indexBy="locale")
61-
*
62-
* @Serializer\Exclude
6360
*/
6461
private $translations;
6562

@@ -72,8 +69,6 @@ class Event
7269
* @var MediaInterface|null
7370
*
7471
* @ORM\ManyToOne(targetEntity="Sulu\Bundle\MediaBundle\Entity\MediaInterface")
75-
*
76-
* @Serializer\Exclude
7772
*/
7873
private $image = null;
7974

@@ -141,33 +136,13 @@ public function getImage(): ?MediaInterface
141136
return $this->image;
142137
}
143138

144-
/**
145-
* @return array<string, mixed>|null
146-
*
147-
* @Serializer\VirtualProperty
148-
* @Serializer\SerializedName("image")
149-
*/
150-
public function getImageData(): ?array
151-
{
152-
if (!$this->image) {
153-
return null;
154-
}
155-
156-
return [
157-
'id' => $this->image->getId(),
158-
];
159-
}
160-
161139
public function setImage(?MediaInterface $image): self
162140
{
163141
$this->image = $image;
164142

165143
return $this;
166144
}
167145

168-
/**
169-
* @Serializer\VirtualProperty(name="title")
170-
*/
171146
public function getTitle(): ?string
172147
{
173148
$translation = $this->getTranslation($this->locale);
@@ -190,9 +165,6 @@ public function setTitle(string $title): self
190165
return $this;
191166
}
192167

193-
/**
194-
* @Serializer\VirtualProperty(name="teaser")
195-
*/
196168
public function getTeaser(): ?string
197169
{
198170
$translation = $this->getTranslation($this->locale);
@@ -215,9 +187,6 @@ public function setTeaser(string $teaser): self
215187
return $this;
216188
}
217189

218-
/**
219-
* @Serializer\VirtualProperty(name="description")
220-
*/
221190
public function getDescription(): ?string
222191
{
223192
$translation = $this->getTranslation($this->locale);

0 commit comments

Comments
 (0)