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
|
7 |
|
public function __construct( |
37
|
|
|
EventRepository $eventRepository, |
38
|
|
|
Router $router, |
39
|
|
|
FileService $fileService |
40
|
|
|
) { |
41
|
7 |
|
$this->eventRepository = $eventRepository; |
42
|
7 |
|
$this->router = $router; |
43
|
7 |
|
$this->fileService = $fileService; |
44
|
7 |
|
} |
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
|
4 |
|
public function addImageToEvent(string $eventId, User $requestExecutor, array $imageData = null): AbstractApiResponse |
94
|
|
|
{ |
95
|
4 |
|
$event = $this->eventRepository->findOneById($eventId); |
96
|
|
|
|
97
|
4 |
|
if ($event) { |
98
|
3 |
|
if ($requestExecutor->getLogin() !== $event->getCreator()->getLogin()) { |
99
|
1 |
|
$response = new ApiError('Only event creator can add images.', Response::HTTP_FORBIDDEN); |
100
|
|
|
} else { |
101
|
2 |
|
$imageName = $imageData['name'] ?: null; |
102
|
2 |
|
$imageContent = $imageData['content'] ?? null; |
103
|
|
|
|
104
|
2 |
|
if (empty($imageData) || !$imageName || !$imageContent) { |
105
|
1 |
|
$response = new ApiError( |
106
|
1 |
|
'Parameters are mandatory: image[name] and image[content].', |
107
|
1 |
|
Response::HTTP_BAD_REQUEST |
108
|
|
|
); |
109
|
|
|
} else { |
110
|
|
|
try { |
111
|
1 |
|
$imageExtension = $this->fileService->getExtensionFromBase64File($imageContent); |
112
|
1 |
|
$imageName = sprintf('%s.%s', $imageName, $imageExtension); |
113
|
1 |
|
$image = $this->fileService->createBase64Image($imageName, $imageContent, $event); |
114
|
|
|
|
115
|
1 |
|
$imageLocation = $this->router->generate( |
116
|
1 |
|
'event_image_view', |
117
|
|
|
[ |
118
|
1 |
|
'eventId' => $eventId, |
119
|
1 |
|
'imageName' => $image->getName(), |
120
|
|
|
], |
121
|
1 |
|
Router::ABSOLUTE_URL |
122
|
|
|
); |
123
|
|
|
|
124
|
1 |
|
$response = new LocationApiResponse(Response::HTTP_OK, $imageLocation); |
125
|
|
|
} catch (UnsupportedTypeException $exception) { |
126
|
|
|
$response = new ApiError( |
127
|
|
|
'Only images of types png, gif and jpeg are supported.', |
128
|
3 |
|
Response::HTTP_BAD_REQUEST |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} else { |
135
|
1 |
|
$response = $this->createEventNotFoundErrorResult($eventId); |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
return $response; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
public function createEventNotFoundErrorResult(string $eventId): ApiError |
142
|
|
|
{ |
143
|
2 |
|
return new ApiError( |
144
|
2 |
|
sprintf('Event with id "%s" was not found.', $eventId), |
145
|
2 |
|
Response::HTTP_NOT_FOUND |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
private function createLocationById(string $eventId): string |
150
|
|
|
{ |
151
|
1 |
|
return $this->router->generate('event_view', ['eventId' => $eventId]); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|