ExecuteMigrationCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\SymfonyTranslationBundle\Cli;
6
7
use Doctrine\Migrations\Finder\GlobFinder;
8
use Locastic\SymfonyTranslationBundle\TranslationMigration\ExecutorInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
use function sprintf;
15
16
final class ExecuteMigrationCommand extends Command
17
{
18
    /** @var string */
19
    public static $defaultName = 'locastic:symfony-translation:migration:migrate';
20
21
    private GlobFinder $translationFinder;
22
23
    private string $translationMigrationDirectory;
24
25
    private ExecutorInterface $migrationExecutor;
26
27
    protected OutputInterface $output;
28
29
    public function __construct(
30
        GlobFinder $translationFinder,
31
        string $translationMigrationDirectory,
32
        ExecutorInterface $migrationExecutor,
33
        string $name = null
34
    ) {
35
        parent::__construct($name);
36
37
        $this->translationFinder = $translationFinder;
38
        $this->translationMigrationDirectory = $translationMigrationDirectory;
39
        $this->migrationExecutor = $migrationExecutor;
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this->setDescription('Execute migration of translations');
45
46
        $this->addOption(
47
            'resync',
48
            'r',
49
            InputOption::VALUE_NONE,
50
            'Replay all migrations to resync missed ones without replacing existing ones'
51
        );
52
    }
53
54
    protected function execute(InputInterface $input, OutputInterface $output): int
55
    {
56
        $this->writeln('Starting to execute migration of translations', OutputInterface::VERBOSITY_NORMAL);
57
58
        $migrations = $this->translationFinder->findMigrations($this->translationMigrationDirectory);
59
        foreach ($migrations as $value) {
60
            $this->writeLn(sprintf('Working with Migration "%s', $value), OutputInterface::VERBOSITY_VERBOSE);
61
            $migration = new $value($this->migrationExecutor);
62
            $resync = $input->getOption('resync');
63
64
            $this->migrationExecutor->execute($migration, $resync);
65
            $this->migrationExecutor->clearTranslations();
66
            usleep(100);
67
        }
68
69
        return 0;
70
    }
71
72
    protected function initialize(InputInterface $input, OutputInterface $output): void
73
    {
74
        parent::initialize($input, $output);
75
76
        $this->output = $output;
77
    }
78
79
    protected function writeLn(string $message, int $level = OutputInterface::OUTPUT_NORMAL): void
80
    {
81
        $this->output->writeln(sprintf('[%s] %s', date('Y-m-d H:i:s'), $message), $level);
82
    }
83
}
84