AddWatchedMovieRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 8
dl 0
loc 40
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 21 3
A getWatchedMovieDTO() 0 10 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