Completed
Push — master ( ffe573...d5b246 )
by Valentyn
03:57
created

FixReleaseDateQueue::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 20
cp 0
rs 8.7377
c 0
b 0
f 0
cc 6
nc 7
nop 2
crap 42
1
<?php
2
3
namespace App\Movies\Command;
4
5
use App\Movies\Entity\ReleaseDateQueue;
6
use App\Movies\Exception\TmdbMovieNotFoundException;
7
use App\Movies\Repository\ReleaseDateQueueRepository;
8
use App\Movies\Service\ImdbIdLoaderService;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class FixReleaseDateQueue extends Command
15
{
16
    private $repository;
17
    private $imdb;
18
    private $em;
19
20
    public function __construct(ImdbIdLoaderService $imdb, ReleaseDateQueueRepository $repository, EntityManagerInterface $em, ?string $name = null)
21
    {
22
        parent::__construct($name);
23
24
        $this->repository = $repository;
25
        $this->imdb = $imdb;
26
        $this->em = $em;
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('app:fix-release-date-queue')
33
            ->setDescription('Check inactive queue items to fix them')
34
            ->setHelp('This command will search for inactive queue items and will try to fix them (load imdb id)');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $queueItems = $this->repository->findAllWithMovies(false)->getResult();
40
41
        $i = 0;
42
        /** @var $queueItem ReleaseDateQueue */
43
        foreach ($queueItems as $queueItem) {
44
            $movie = $queueItem->getMovie();
45
            $tmdbId = $movie->getTmdb()->getId();
46
47
            if ($movie->getImdbId()) {
48
                continue;
49
            }
50
51
            try {
52
                $imdbId = $this->imdb->getImdbId($tmdbId);
53
            } catch (TmdbMovieNotFoundException $e) {
54
                continue;
55
            }
56
57
            if ($imdbId !== null ) {
58
                $queueItem->activate();
59
                $movie->setImdbId($imdbId);
60
            }
61
62
            if ($i === 5) {
63
                $i = 0;
64
                sleep(5);
65
            }
66
67
            ++$i;
68
        }
69
70
        $this->em->flush();
71
    }
72
}
73