1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\Request; |
4
|
|
|
|
5
|
|
|
use App\Movies\DTO\WatchedMovieDTO; |
6
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
7
|
|
|
use App\Request\BaseRequest; |
8
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
9
|
|
|
|
10
|
|
|
class AddWatchedMovieRequest extends BaseRequest |
11
|
|
|
{ |
12
|
|
|
public function rules() |
13
|
|
|
{ |
14
|
6 |
|
$movieIdRequired = function ($object, ExecutionContextInterface $context, $payload) { |
|
|
|
|
15
|
6 |
|
$data = $context->getRoot()['movie']; |
16
|
6 |
|
$tmdb_id = $data['tmdbId'] ?? null; |
17
|
6 |
|
$id = $data['id'] ?? null; |
18
|
|
|
|
19
|
6 |
|
if (empty($tmdb_id) && empty($id)) { |
20
|
1 |
|
$context->buildViolation('Movie Id or TMDB Id should be provided')->addViolation(); |
21
|
|
|
} |
22
|
6 |
|
}; |
23
|
|
|
|
24
|
6 |
|
return new Assert\Collection([ |
25
|
6 |
|
'movie' => new Assert\Collection([ |
26
|
6 |
|
'id' => new Assert\Callback($movieIdRequired), |
27
|
6 |
|
'tmdbId' => new Assert\Callback($movieIdRequired), |
28
|
6 |
|
'vote' => [new Assert\Range(['min' => 0.0, 'max' => 10.0])], |
29
|
6 |
|
'watchedAt' => [new Assert\Date()], |
30
|
|
|
]), |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return WatchedMovieDTO |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
5 |
|
public function getWatchedMovieDTO() |
39
|
|
|
{ |
40
|
5 |
|
$movieData = $this->get('movie'); |
41
|
5 |
|
$movieId = (int)$movieData['id'] ?? null; |
42
|
5 |
|
$movieTmdbId = (int)$movieData['tmdbId'] ?? null; |
43
|
5 |
|
$vote = (float)$movieData['vote'] ?? null; |
44
|
5 |
|
$watchedAt = !empty($movieData['watchedAt']) ? new \DateTimeImmutable($movieData['watchedAt']) : null; |
45
|
|
|
|
46
|
5 |
|
return new WatchedMovieDTO($movieId, $movieTmdbId, $vote, $watchedAt); |
47
|
|
|
} |
48
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.