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) { |
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
|
|
|
$data['title'] = substr($data['title'], 0, 99); |
61
|
|
|
|
62
|
|
|
$movie->addTranslation( |
63
|
|
|
new MovieTranslations( |
64
|
|
|
$movie, |
65
|
|
|
new MovieTranslationDTO('uk', $data['title'], $data['overview'], null) |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$o->writeln("Added translation for {$movie->getOriginalTitle()}"); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$this->em->persist($movie); |
73
|
|
|
$this->em->flush(); |
74
|
|
|
} catch (\Throwable $e) { |
75
|
|
|
$o->writeln("Exception: {$e->getMessage()}") |
76
|
|
|
} |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |