ReleaseDateService::runCheck()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 53
c 0
b 0
f 0
ccs 0
cts 31
cp 0
rs 8.0921
cc 7
nc 14
nop 0
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Psr\Log\LoggerInterface;
14
15
class ReleaseDateService
16
{
17
    private $em;
18
    private $repository;
19
    private $imdbReleaseDateService;
20
    private $countryRepository;
21
    private $countries;
22
    private $logger;
23
24
    public function __construct(EntityManagerInterface $em, ReleaseDateQueueRepository $repository, ImdbReleaseDateService $imdbReleaseDateService, CountryRepository $countryRepository, LoggerInterface $logger)
25
    {
26
        $this->em = $em;
27
        $this->repository = $repository;
28
        $this->imdbReleaseDateService = $imdbReleaseDateService;
29
        $this->countryRepository = $countryRepository;
30
        $this->logger = $logger;
31
    }
32
33
    public function runCheck(): void
34
    {
35
        $queueItems = $this->repository->findAllWithMovies()->getResult();
36
        $this->countries = $this->countryRepository->findAll();
37
38
        $this->logger->debug('[ReleaseDateService] runCheck', [
39
            'count' => \count($queueItems),
40
        ]);
41
42
        /** @var $queueItem ReleaseDateQueue */
43
        foreach ($queueItems as $queueItem) {
44
            $allCountriesHaveReleaseDate = true;
45
            $movie = $queueItem->getMovie();
46
            foreach ($this->countries as $country) {
47
                $releaseDate = $this->imdbReleaseDateService->getReleaseDate($movie, $country);
48
49
                $this->logger->debug('[ReleaseDateService] trying to load release date for movie', [
50
                    'movie' => sprintf('%s with id: %s', $movie->getOriginalTitle(), $movie->getId()),
51
                    'releaseDate' => $releaseDate ? $releaseDate->format('Y-m-d') : '[EMPTY]',
52
                ]);
53
54
                if ($releaseDate === null) {
55
                    $allCountriesHaveReleaseDate = false;
56
                } else {
57
                    $this->logger->debug(
58
                        sprintf('[ReleaseDateService] Added new release date %s for movie %s in country %s',
59
                            $releaseDate->format('Y-m-d'),
60
                            $movie->getOriginalTitle(),
61
                            $country->getName()
62
                        )
63
                    );
64
                    $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 47 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...
65
                    $this->em->persist($movieReleaseDate);
66
                }
67
            }
68
69
            if ($allCountriesHaveReleaseDate === true) {
70
                $this->logger->debug(
71
                    sprintf('[ReleaseDateService] Movie %s removed from queue. All available countries already have release dates',
72
                        $movie->getOriginalTitle()
73
                    )
74
                );
75
                $this->em->remove($queueItem);
76
            }
77
        }
78
79
        try {
80
            $this->em->flush();
81
        } catch (UniqueConstraintViolationException $exception) {
82
            // If release date was saved early
83
            $this->logger->debug("[ReleaseDateService] Exception with message: {$exception->getMessage()}");
84
        }
85
    }
86
}
87