Completed
Push — master ( 1a9043...984777 )
by Valentyn
08:31
created

AddWatchedMovieRequest::getWatchedMovieDTO()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

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.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
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) {
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 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
}