AddWatchedMovieRequest::getWatchedMovieDTO()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $payload is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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