1 | <?php |
||
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()}") |
||
79 | } |