Completed
Push — master ( 7062b3...1c7f62 )
by
unknown
01:56 queued 39s
created

execute()   B

Complexity

Conditions 8
Paths 186

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
c 1
b 0
f 0
nc 186
nop 2
dl 0
loc 42
rs 7.7277
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Command;
8
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
final class DoctrineMigrationsMigrateCommandDecorator extends Command
16
{
17
    public const SKIP_ATTENDANCES_FLAG = 'CHAMILO_MIGRATION_SKIP_ATTENDANCES';
18
19
    public function __construct(private readonly Command $inner)
20
    {
21
        parent::__construct($inner->getName() ?: 'doctrine:migrations:migrate');
22
    }
23
24
    protected function configure(): void
25
    {
26
        $this->setName($this->inner->getName() ?: 'doctrine:migrations:migrate');
27
        $this->setDescription((string) $this->inner->getDescription());
28
        $this->setHelp((string) $this->inner->getHelp());
29
30
        $definition = clone $this->inner->getDefinition();
31
        $this->setDefinition($definition);
32
33
        $this->addOption(
34
            'skip-attendances',
35
            null,
36
            InputOption::VALUE_NONE,
37
            'When enabled, only migrate attendances linked to gradebook (type=7). Others will be skipped.'
38
        );
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        $skipAttendances = (bool) $input->getOption('skip-attendances');
44
45
        if ($skipAttendances) {
46
            $_SERVER[self::SKIP_ATTENDANCES_FLAG] = '1';
47
            $_ENV[self::SKIP_ATTENDANCES_FLAG] = '1';
48
            putenv(self::SKIP_ATTENDANCES_FLAG.'=1');
49
        }
50
51
        try {
52
            $innerDefinition = $this->inner->getDefinition();
53
            $data = [];
54
55
            foreach ($innerDefinition->getArguments() as $name => $argument) {
56
                $value = $input->getArgument($name);
57
                if (null !== $value) {
58
                    $data[$name] = $value;
59
                }
60
            }
61
62
            foreach ($innerDefinition->getOptions() as $name => $option) {
63
                $value = $input->getOption($name);
64
65
                if ($option->acceptValue()) {
66
                    if (null !== $value) {
67
                        $data['--'.$name] = $value;
68
                    }
69
                } else {
70
                    if (true === $value) {
71
                        $data['--'.$name] = true;
72
                    }
73
                }
74
            }
75
76
            $innerInput = new ArrayInput($data, $innerDefinition);
77
            $innerInput->setInteractive($input->isInteractive());
78
79
            return $this->inner->run($innerInput, $output);
80
        } finally {
81
            unset($_SERVER[self::SKIP_ATTENDANCES_FLAG], $_ENV[self::SKIP_ATTENDANCES_FLAG]);
82
            putenv(self::SKIP_ATTENDANCES_FLAG);
83
        }
84
    }
85
}
86