Completed
Pull Request — master (#138)
by Loïc
02:53
created

RunCommands::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\Console\Output\NullOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
trait RunCommands
21
{
22
    use CreateProgressBar;
23
24
    /**
25
     * @var CommandExecutor
26
     */
27
    private $commandExecutor;
28
29
    /**
30
     * @var EntityManagerInterface
31
     */
32
    private $entityManager;
33
34
    /**
35
     * @param CommandExecutor $commandExecutor
36
     * @param ObjectManager   $entityManager
37
     */
38
    public function __construct(CommandExecutor $commandExecutor, ObjectManager $entityManager)
39
    {
40
        $this->commandExecutor = $commandExecutor;
41
        $this->entityManager = $entityManager;
0 ignored issues
show
Documentation Bug introduced by
$entityManager is of type object<Doctrine\Common\Persistence\ObjectManager>, but the property $entityManager was declared to be of type object<Doctrine\ORM\EntityManagerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
42
    }
43
44
    /**
45
     * @param array           $commands
46
     * @param OutputInterface $output
47
     * @param bool            $displayProgress
48
     *
49
     * @throws \Exception
50
     */
51
    protected function runCommands(array $commands, OutputInterface $output, bool $displayProgress = true): void
52
    {
53
        $progress = $this->createProgressBar($displayProgress ? $output : new NullOutput(), count($commands));
54
55
        foreach ($commands as $key => $value) {
56
            if (is_string($key)) {
57
                $command = $key;
58
                $parameters = $value;
59
            } else {
60
                $command = $value;
61
                $parameters = [];
62
            }
63
64
            $this->commandExecutor->runCommand($command, $parameters);
65
66
            // PDO does not always close the connection after Doctrine commands.
67
            // See https://github.com/symfony/symfony/issues/11750.
68
            $this->entityManager->getConnection()->close();
69
70
            $progress->advance();
71
        }
72
73
        $progress->finish();
74
    }
75
}
76