|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\DTO\CreateEventDTO; |
|
6
|
|
|
use AppBundle\Entity\Event; |
|
7
|
|
|
use AppBundle\Entity\Repository\EventRepository; |
|
8
|
|
|
use AppBundle\Entity\User; |
|
9
|
|
|
use AppBundle\Exception\UnsupportedTypeException; |
|
10
|
|
|
use AppBundle\Response\ApiError; |
|
11
|
|
|
use AppBundle\Response\CreatedApiResponse; |
|
12
|
|
|
use AppBundle\Response\EmptyApiResponse; |
|
13
|
|
|
use AppBundle\Response\Infrastructure\AbstractApiResponse; |
|
14
|
|
|
use AppBundle\Response\LocationApiResponse; |
|
15
|
|
|
use AppBundle\Service\File\FileService; |
|
16
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
17
|
|
|
use Symfony\Component\Form\FormInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\Routing\Router; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Vehsamrak |
|
23
|
|
|
*/ |
|
24
|
|
|
class EventService |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** @var EventRepository */ |
|
28
|
|
|
private $eventRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Router */ |
|
31
|
|
|
private $router; |
|
32
|
|
|
|
|
33
|
|
|
/** @var FileService */ |
|
34
|
|
|
private $fileService; |
|
35
|
|
|
|
|
36
|
6 |
|
public function __construct( |
|
37
|
|
|
EventRepository $eventRepository, |
|
38
|
|
|
Router $router, |
|
39
|
|
|
FileService $fileService |
|
40
|
|
|
) { |
|
41
|
6 |
|
$this->eventRepository = $eventRepository; |
|
42
|
6 |
|
$this->router = $router; |
|
43
|
6 |
|
$this->fileService = $fileService; |
|
44
|
6 |
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function createEventByForm(FormInterface $form, User $creator): AbstractApiResponse |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var CreateEventDTO $createEventDTO */ |
|
49
|
1 |
|
$createEventDTO = $form->getData(); |
|
50
|
|
|
|
|
51
|
1 |
|
$newEvent = new Event($createEventDTO->name, $creator, $createEventDTO->date, $createEventDTO->description); |
|
52
|
|
|
|
|
53
|
1 |
|
$this->eventRepository->persist($newEvent); |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
1 |
|
$this->eventRepository->flush(); |
|
57
|
1 |
|
$response = new CreatedApiResponse($this->createLocationById($newEvent->getId())); |
|
58
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
|
59
|
|
|
$response = new ApiError('Event must have unique name and date.', Response::HTTP_BAD_REQUEST); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $response; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function editEventByForm(FormInterface $form, string $eventId): AbstractApiResponse |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var Event $event */ |
|
68
|
1 |
|
$event = $this->eventRepository->findOneById($eventId); |
|
69
|
|
|
|
|
70
|
1 |
|
if (!$event) { |
|
71
|
|
|
$response = $this->createEventNotFoundErrorResult($eventId); |
|
72
|
|
|
} else { |
|
73
|
1 |
|
$eventName = $form->get('name')->getData(); |
|
74
|
|
|
/** @var \DateTime $eventDate */ |
|
75
|
1 |
|
$eventDate = $form->get('date')->getData(); |
|
76
|
1 |
|
$eventDescription = $form->get('description')->getData(); |
|
77
|
|
|
|
|
78
|
1 |
|
$event->setName($eventName); |
|
79
|
1 |
|
$event->setDate($eventDate); |
|
80
|
1 |
|
$event->setDescription($eventDescription); |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
1 |
|
$this->eventRepository->flush(); |
|
84
|
1 |
|
$response = new EmptyApiResponse(Response::HTTP_NO_CONTENT); |
|
85
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
|
86
|
|
|
$response = new ApiError('Event must have unique name and date.', Response::HTTP_BAD_REQUEST); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
public function addImageToEvent(string $eventId, array $imageData = null): AbstractApiResponse |
|
94
|
|
|
{ |
|
95
|
3 |
|
$event = $this->eventRepository->findOneById($eventId); |
|
96
|
|
|
|
|
97
|
3 |
|
if ($event) { |
|
98
|
2 |
|
$imageName = $imageData['name'] ?: null; |
|
99
|
2 |
|
$imageContent = $imageData['content'] ?? null; |
|
100
|
|
|
|
|
101
|
2 |
|
if (!$imageData || !$imageName || !$imageContent) { |
|
|
|
|
|
|
102
|
1 |
|
$response = new ApiError( |
|
103
|
1 |
|
'Parameters are mandatory: image[name] and image[content].', |
|
104
|
1 |
|
Response::HTTP_BAD_REQUEST |
|
105
|
|
|
); |
|
106
|
|
|
} else { |
|
107
|
|
|
try { |
|
108
|
1 |
|
$imageExtension = $this->fileService->getExtensionFromBase64File($imageContent); |
|
109
|
1 |
|
$imageName = sprintf('%s.%s', $imageName, $imageExtension); |
|
110
|
1 |
|
$image = $this->fileService->createBase64Image($imageName, $imageContent, $event); |
|
111
|
|
|
|
|
112
|
1 |
|
$imageLocation = $this->router->generate( |
|
113
|
1 |
|
'event_image_view', |
|
114
|
|
|
[ |
|
115
|
1 |
|
'eventId' => $eventId, |
|
116
|
1 |
|
'imageName' => $image->getName(), |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
1 |
|
$response = new LocationApiResponse(Response::HTTP_OK, $imageLocation); |
|
121
|
|
|
} catch (UnsupportedTypeException $exception) { |
|
122
|
|
|
$response = new ApiError( |
|
123
|
|
|
'Only images of types png, gif and jpeg are supported.', |
|
124
|
2 |
|
Response::HTTP_BAD_REQUEST |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
1 |
|
$response = $this->createEventNotFoundErrorResult($eventId); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
3 |
|
return $response; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
public function createEventNotFoundErrorResult(string $eventId): ApiError |
|
136
|
|
|
{ |
|
137
|
2 |
|
return new ApiError( |
|
138
|
2 |
|
sprintf('Event with id "%s" was not found.', $eventId), |
|
139
|
2 |
|
Response::HTTP_NOT_FOUND |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
private function createLocationById(string $eventId): string |
|
144
|
|
|
{ |
|
145
|
1 |
|
return $this->router->generate('event_view', ['eventId' => $eventId]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.