Completed
Push — master ( df0c35...7f3dc3 )
by Valentyn
03:58
created

LoadTranslations::execute()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 0
cts 24
cp 0
rs 8.6257
c 0
b 0
f 0
cc 6
nc 9
nop 2
crap 42
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 Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class LoadTranslations extends Command
17
{
18
    /** @var $repository MovieReleaseDateRepository */
19
    private $repository;
20
21
    /** @var $parser Kinobaza */
22
    private $parser;
23
24
    private $em;
25
26
    public function __construct(EntityManagerInterface $em, MovieRepository $repository, Kinobaza $parser, ?string $name = null)
27
    {
28
        parent::__construct($name);
29
30
        $this->repository = $repository;
31
        $this->parser = $parser;
32
        $this->em = $em;
33
    }
34
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('app:load-translations')
39
            ->setDescription('Find movies without translation for Ukrainian and try to find it')
40
            ->setHelp('php bin/console app:load-translations');
41
    }
42
43
    protected function execute(InputInterface $i, OutputInterface $o)
44
    {
45
        $movies = $this->repository->findAllWithEmptyTranslation('uk');
46
        $movies = $movies->getResult();
47
48
        /** @var $movie Movie */
49
        foreach ($movies as $movie) {
50
            if (null === $releaseDate = $movie->getReleaseDate()) {
51
                $o->writeln(sprintf('Movie "%s" dont have release date. Skipping...', $movie->getOriginalTitle()));
52
                continue;
53
            }
54
55
            $data = $this->parser->find($movie->getOriginalTitle(), (int)$releaseDate->format('Y'));
56
57
            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...
58
                $o->writeln(sprintf('Cant find movie "%s" in kinobaza.com.ua', $movie->getOriginalTitle()));
59
                continue;
60
            }
61
62
            if (!empty($data['overview'])) {
63
                $data['overview'] .= "\nДжерело https://kinobaza.com.ua";
64
            }
65
66
            $data['title'] = substr($data['title'], 0, 99);
67
68
            $movie->addTranslation(
69
                new MovieTranslations(
70
                    $movie,
71
                    new MovieTranslationDTO('uk', $data['title'], $data['overview'], null)
72
                )
73
            );
74
75
            $o->writeln("Added translation for {$movie->getOriginalTitle()}");
76
77
            try {
78
                $this->em->persist($movie);
79
                $this->em->flush();
80
            } catch (\Throwable $e) {
81
                $o->writeln("Exception: {$e->getMessage()}");
82
            }
83
        }
84
    }
85
}