Completed
Branch master (69c390)
by Loïc
08:01 queued 02:54
created

ensureDirectoryExistsAndIsWritable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 AppBundle\Command\Installer;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Helper\Table;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
abstract class AbstractInstallCommand extends ContainerAwareCommand
22
{
23
    const WEB_ASSETS_DIRECTORY = 'web/assets/';
24
    const WEB_BUNDLES_DIRECTORY = 'web/bundles/';
25
    const WEB_MEDIA_DIRECTORY = 'web/media/';
26
    const WEB_MEDIA_IMAGE_DIRECTORY = 'web/media/image/';
27
28
    /**
29
     * @var CommandExecutor
30
     */
31
    protected $commandExecutor;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function initialize(InputInterface $input, OutputInterface $output)
37
    {
38
        $application = $this->getApplication();
39
        $application->setCatchExceptions(false);
40
41
        $this->commandExecutor = new CommandExecutor($input, $output, $application);
0 ignored issues
show
Bug introduced by
It seems like $application defined by $this->getApplication() on line 38 can be null; however, AppBundle\Command\Instal...Executor::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
    }
43
44
    /**
45
     * @param string $id
46
     *
47
     * @return object
48
     */
49
    protected function get($id)
50
    {
51
        return $this->getContainer()->get($id);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    protected function getEnvironment()
58
    {
59
        return $this->get('kernel')->getEnvironment();
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    protected function isDebug()
66
    {
67
        return $this->get('kernel')->isDebug();
68
    }
69
70
    /**
71
     * @param array $headers
72
     * @param array $rows
73
     * @param OutputInterface $output
74
     */
75
    protected function renderTable(array $headers, array $rows, OutputInterface $output)
76
    {
77
        $table = new Table($output);
78
79
        $table
80
            ->setHeaders($headers)
81
            ->setRows($rows)
82
            ->render()
83
        ;
84
    }
85
86
    /**
87
     * @param OutputInterface $output
88
     * @param int $length
89
     *
90
     * @return ProgressBar
91
     */
92
    protected function createProgressBar(OutputInterface $output, $length = 10)
0 ignored issues
show
Unused Code introduced by
The parameter $length is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        ProgressBar::setFormatDefinition('custom', ' %current%/%max% [%bar%] %percent:3s%% <info>%command_name%</info>');
95
96
        $progress = new ProgressBar($output);
97
        $progress->setFormat('custom');
98
        $progress->setBarCharacter('<info>|</info>');
99
        $progress->setBarCharacter('<info>|</info>');
100
        $progress->setEmptyBarCharacter(' ');
101
        $progress->setProgressCharacter('|');
102
103
        return $progress;
104
    }
105
106
    /**
107
     * @param string $commandName
108
     *
109
     * @return \Closure
110
     */
111
    protected function getCommandDescriptionClosure($commandName)
112
    {
113
        return function() use ($commandName) {
114
            /** @var Command $command */
115
            $command = $this->getApplication()->find($commandName);
116
117
            return $command->getDescription();
118
        };
119
    }
120
121
    /**
122
     * @param array $commands
123
     * @param OutputInterface $output
124
     */
125
    protected function runCommands(array $commands, OutputInterface $output)
126
    {
127
        $length = count($commands);
128
        $progress = $this->createProgressBar($output, $length);
129
        $started = false;
130
131
        foreach ($commands as $key => $value) {
132
            if (is_string($key)) {
133
                $command = $key;
134
                $parameters = $value;
135
            } else {
136
                $command = $value;
137
                $parameters = [];
138
            }
139
140
            ProgressBar::setPlaceholderFormatterDefinition('command_name', $this->getCommandDescriptionClosure($command));
141
142
            if (!$started) {
143
                $progress->start($length);
144
                $started = true;
145
            } else {
146
                $progress->advance();
147
            }
148
149
            $this->commandExecutor->runCommand($command, $parameters);
150
151
            // PDO does not always close the connection after Doctrine commands.
152
            // See https://github.com/symfony/symfony/issues/11750.
153
            $this->get('doctrine')->getManager()->getConnection()->close();
154
        }
155
156
        $progress->finish();
157
    }
158
159
    /**
160
     * @param string $directory
161
     * @param OutputInterface $output
162
     */
163
    protected function ensureDirectoryExistsAndIsWritable($directory, OutputInterface $output)
164
    {
165
        $checker = $this->get('sylius.installer.checker.command_directory');
166
        $checker->setCommandName($this->getName());
167
168
        $checker->ensureDirectoryExists($directory, $output);
169
        $checker->ensureDirectoryIsWritable($directory, $output);
170
    }
171
}
172