Completed
Push — master ( 2044f4...a35418 )
by Loïc
13s queued 11s
created

CommandsRunner::run()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 3
nop 5
1
<?php
2
3
/*
4
 * This file is part of monofony.
5
 *
6
 * (c) Mobizel
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Command\Helper;
15
16
use App\Command\Installer\CommandExecutor;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\NullOutput;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
final class CommandsRunner
24
{
25
    /** @var EntityManagerInterface */
26
    private $entityManager;
27
28
    /** @var ProgressBarCreator */
29
    private $progressBarCreator;
30
31
    public function __construct(
32
        EntityManagerInterface $entityManager,
33
        ProgressBarCreator $progressBarCreator
34
    ) {
35
        $this->entityManager = $entityManager;
36
        $this->progressBarCreator = $progressBarCreator;
37
    }
38
39
    /**
40
     * @throws \Exception
41
     */
42
    public function run(array $commands, InputInterface $input, OutputInterface $output, Application $application, bool $displayProgress = true): void
43
    {
44
        $progress = $this->progressBarCreator->create($displayProgress ? $output : new NullOutput(), count($commands));
45
        $commandExecutor = new CommandExecutor($input, $output, $application);
46
47
        foreach ($commands as $key => $value) {
48
            if (is_string($key)) {
49
                $command = $key;
50
                $parameters = $value;
51
            } else {
52
                $command = $value;
53
                $parameters = [];
54
            }
55
56
            $commandExecutor->runCommand($command, $parameters);
57
58
            // PDO does not always close the connection after Doctrine commands.
59
            // See https://github.com/symfony/symfony/issues/11750.
60
            $this->entityManager->getConnection()->close();
61
62
            $progress->advance();
63
        }
64
65
        $progress->finish();
66
    }
67
}
68