Completed
Push — master ( 9e132c...a87553 )
by Valentyn
04:13
created

LoadTranslations   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 7 1
A execute() 0 32 4
1
<?php
2
3
namespace App\Movies\Command;
4
5
use App\Movies\DTO\MovieTranslationDTO;
6
use App\Movies\Entity\MovieTranslations;
7
use App\Movies\Parser\Kinobaza;
8
use App\Movies\Repository\MovieReleaseDateRepository;
9
use App\Movies\Repository\MovieRepository;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class LoadTranslations extends Command
16
{
17
    /** @var $repository MovieReleaseDateRepository */
18
    private $repository;
19
20
    /** @var $parser Kinobaza */
21
    private $parser;
22
23
    private $em;
24
25
    public function __construct(EntityManagerInterface $em, MovieRepository $repository, Kinobaza $parser, ?string $name = null)
26
    {
27
        parent::__construct($name);
28
29
        $this->repository = $repository;
30
        $this->parser = $parser;
31
        $this->em = $em;
32
    }
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('app:load-translations')
38
            ->setDescription('Find movies without translation for Ukrainian and try to find it')
39
            ->setHelp('php bin/console app:load-translations');
40
    }
41
42
    protected function execute(InputInterface $i, OutputInterface $o)
43
    {
44
        $movies = $this->repository->findAllWithEmptyTranslation('uk');
45
46
        foreach ($movies as $movie) {
47
            if (null === $releaseDate = $movie->getReleaseDate()) {
48
                $o->writeln(sprintf('Movie "%s" dont have release date. Skipping...', $movie->getOriginalTitle()));
49
                continue;
50
            }
51
52
            $data = $this->parser->find($movie->getOriginalTitle(), (int)$releaseDate->format('Y'));
53
54
            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...
55
                $o->writeln(sprintf('Cant find movie "%s" in kinobaza.com.ua', $movie->getOriginalTitle()));
56
                continue;
57
            }
58
59
            $data['overview'] .= "\nДжерело https://kinobaza.com.ua";
60
61
            $movie->addTranslation(
62
                new MovieTranslations(
63
                    $movie,
64
                    new MovieTranslationDTO('uk', $data['title'], $data['overview'], null)
65
                )
66
            );
67
68
            $o->writeln("Added translation for {$movie->getOriginalTitle()}");
69
70
            $this->em->persist($movie);
71
            $this->em->flush();
72
        }
73
    }
74
}