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