LoadTranslations::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Movies\Command;
4
5
use App\Movies\DTO\MovieTranslationDTO;
6
use App\Movies\Entity\Movie;
7
use App\Movies\Entity\MovieTranslations;
8
use App\Movies\Parser\Kinobaza;
9
use App\Movies\Repository\MovieReleaseDateRepository;
10
use App\Movies\Repository\MovieRepository;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Psr\SimpleCache\CacheInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class LoadTranslations extends Command
18
{
19
    /** @var $repository MovieReleaseDateRepository */
20
    private $repository;
21
22
    /** @var $parser Kinobaza */
23
    private $parser;
24
25
    private $em;
26
    private $cache;
27
28
    private const CACHE_TIME = 2592000; // 30 days
29
30
    public function __construct(EntityManagerInterface $em, MovieRepository $repository, Kinobaza $parser, CacheInterface $cache, ?string $name = null)
31
    {
32
        parent::__construct($name);
33
34
        $this->repository = $repository;
35
        $this->parser = $parser;
36
        $this->cache = $cache;
37
        $this->em = $em;
38
    }
39
40
    protected function configure()
41
    {
42
        $this
43
            ->setName('app:load-translations')
44
            ->setDescription('Find movies without translation for Ukrainian and try to find it')
45
            ->setHelp('php bin/console app:load-translations');
46
    }
47
48
    protected function execute(InputInterface $i, OutputInterface $o)
49
    {
50
        $movies = $this->repository->findAllWithEmptyTranslation('uk');
51
        $movies = $movies->getResult();
52
53
        /** @var $movie Movie */
54
        foreach ($movies as $movie) {
55
            $cacheKey = 'notfound_' . $movie->getId();
56
            if ($this->cache->has($cacheKey)) {
57
                continue;
58
            }
59
60
            if (null === $releaseDate = $movie->getReleaseDate()) {
61
                $o->writeln(sprintf('Movie "%s" dont have release date. Skipping...', $movie->getOriginalTitle()));
62
                continue;
63
            }
64
65
            $data = $this->parser->find($movie->getOriginalTitle(), (int)$releaseDate->format('Y'));
66
67
            if (!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
                $this->cache->set($cacheKey, true, self::CACHE_TIME);
69
                $o->writeln(sprintf('Cant find movie "%s" in kinobaza.com.ua', $movie->getOriginalTitle()));
70
                continue;
71
            }
72
73
            if (!empty($data['overview'])) {
74
                $data['overview'] .= "\nДжерело https://kinobaza.com.ua";
75
            }
76
77
            $data['title'] = substr($data['title'], 0, 99);
78
79
            $movie->addTranslation(
80
                new MovieTranslations(
81
                    $movie,
82
                    new MovieTranslationDTO('uk', $data['title'], $data['overview'], null)
83
                )
84
            );
85
86
            $o->writeln("Added translation for {$movie->getOriginalTitle()}");
87
88
            try {
89
                $this->em->persist($movie);
90
                $this->em->flush();
91
            } catch (\Throwable $e) {
92
                $this->cache->set($cacheKey, true, self::CACHE_TIME);
93
                $o->writeln("Exception: {$e->getMessage()}");
94
                throw $e;
95
            }
96
        }
97
    }
98
}