Completed
Push — master ( c5110b...a167e6 )
by Valentyn
13:46
created

ReleaseDateService::runCheck()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 16
cp 0
rs 8.8337
c 0
b 0
f 0
cc 6
nc 14
nop 0
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Service;
6
7
use App\Countries\Repository\CountryRepository;
8
use App\Movies\Entity\MovieReleaseDate;
9
use App\Movies\Entity\ReleaseDateQueue;
10
use App\Movies\Repository\ReleaseDateQueueRepository;
11
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
12
use Doctrine\ORM\EntityManagerInterface;
13
14
class ReleaseDateService
15
{
16
    private $em;
17
    private $repository;
18
    private $imdbReleaseDateService;
19
    private $countries;
20
21
    public function __construct(EntityManagerInterface $em, ReleaseDateQueueRepository $repository, ImdbReleaseDateService $imdbReleaseDateService, CountryRepository $countryRepository)
22
    {
23
        $this->em = $em;
24
        $this->repository = $repository;
25
        $this->imdbReleaseDateService = $imdbReleaseDateService;
26
        $this->countries = $countryRepository->findAll();
27
    }
28
29
    public function runCheck(): void
30
    {
31
        $queueItems = $this->repository->findAllWithMovies()->getResult();
32
33
        /** @var $queueItem ReleaseDateQueue */
34
        foreach ($queueItems as $queueItem) {
35
            $allCountriesHaveReleaseDate = true;
36
            foreach ($this->countries as $country) {
37
                $movie = $queueItem->getMovie();
38
                $releaseDate = $this->imdbReleaseDateService->getReleaseDate($movie, $country);
39
                if ($releaseDate === null) {
40
                    $allCountriesHaveReleaseDate = false;
41
                } else {
42
                    $movieReleaseDate = new MovieReleaseDate($movie, $country, $releaseDate);
0 ignored issues
show
Security Bug introduced by
It seems like $releaseDate defined by $this->imdbReleaseDateSe...eDate($movie, $country) on line 38 can also be of type false; however, App\Movies\Entity\MovieReleaseDate::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
43
                    $this->em->persist($movieReleaseDate);
44
                }
45
            }
46
47
            if ($allCountriesHaveReleaseDate === true) {
48
                $this->em->remove($queueItem);
49
            }
50
        }
51
52
        try {
53
            $this->em->flush();
54
        } catch (UniqueConstraintViolationException $exception) {
55
            // If release date was saved early
56
        }
57
    }
58
}
59