Completed
Pull Request — master (#138)
by Loïc
04:36 queued 01:35
created

RunCommands::runCommands()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 3
nop 3
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
namespace App\Command\Helper;
13
14
use App\Command\Installer\CommandExecutor;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
trait RunCommands
20
{
21
    use CreateProgressBar;
22
23
    /**
24
     * @var CommandExecutor
25
     */
26
    private $commandExecutor;
27
28
    /**
29
     * @var EntityManagerInterface
30
     */
31
    private $entityManager;
32
33
    /**
34
     * @param CommandExecutor        $commandExecutor
35
     * @param EntityManagerInterface $entityManager
36
     */
37
    public function __construct(CommandExecutor $commandExecutor, EntityManagerInterface $entityManager)
38
    {
39
        $this->commandExecutor = $commandExecutor;
40
        $this->entityManager = $entityManager;
41
    }
42
43
    /**
44
     * @param array           $commands
45
     * @param OutputInterface $output
46
     * @param bool            $displayProgress
47
     *
48
     * @throws \Exception
49
     */
50
    private function runCommands(array $commands, OutputInterface $output, bool $displayProgress = true): void
51
    {
52
        $progress = $this->createProgressBar($displayProgress ? $output : new NullOutput(), count($commands));
53
54
        foreach ($commands as $key => $value) {
55
            if (is_string($key)) {
56
                $command = $key;
57
                $parameters = $value;
58
            } else {
59
                $command = $value;
60
                $parameters = [];
61
            }
62
63
            $this->commandExecutor->runCommand($command, $parameters);
64
65
            // PDO does not always close the connection after Doctrine commands.
66
            // See https://github.com/symfony/symfony/issues/11750.
67
            $this->entityManager->getConnection()->close();
68
69
            $progress->advance();
70
        }
71
72
        $progress->finish();
73
    }
74
}
75