Completed
Push — master ( 825767...a283c3 )
by Olivier
02:15
created

ScriptHandler::vhostDefine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\Composer;
4
5
use Composer\Script\CommandEvent;
6
use Symfony\Component\Process\PhpExecutableFinder;
7
use Symfony\Component\Process\Process;
8
9
class ScriptHandler
10
{
11
12
    /**
13
     * Define rabbitmq vhost
14
     *
15
     * @param $event CommandEvent A instance
16
     */
17
    public static function vhostDefine(CommandEvent $event)
18
    {
19
        $options = static::getOptions($event);
0 ignored issues
show
Bug introduced by
The method getOptions() does not seem to exist on object<Ola\RabbitMqAdmin...Composer\ScriptHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
20
        $consoleDir = static::getConsoleDir($event, 'define rabbitmq vhost');
0 ignored issues
show
Bug introduced by
The method getConsoleDir() does not seem to exist on object<Ola\RabbitMqAdmin...Composer\ScriptHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
22
        if (null === $consoleDir) {
23
            return;
24
        }
25
26
        $vhost = '';
27
        if (!empty($options['vhost'])) {
28
            $vhost .= $options['vhost'];
29
        }
30
31
        static::executeCommand($event, $consoleDir, 'rabbitmq:vhost:define '.$vhost, $options['process-timeout']);
32
    }
33
34
    protected static function executeCommand(CommandEvent $event, $consoleDir, $cmd, $timeout = 300)
35
    {
36
        $php = escapeshellarg(static::getPhp(false));
0 ignored issues
show
Bug introduced by
The method getPhp() does not exist on Ola\RabbitMqAdminToolkit...\Composer\ScriptHandler. Did you maybe mean getPhpArguments()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
        $phpArgs = implode(' ', array_map('escapeshellarg', static::getPhpArguments()));
38
        $console = escapeshellarg($consoleDir.'/console');
39
        if ($event->getIO()->isDecorated()) {
40
            $console .= ' --ansi';
41
        }
42
43
        $process = new Process($php.($phpArgs ? ' '.$phpArgs : '').' '.$console.' '.$cmd, null, null, null, $timeout);
44
        $process->run(function ($type, $buffer) use ($event) { $event->getIO()->write($buffer, false); });
45
        if (!$process->isSuccessful()) {
46
            throw new \RuntimeException(sprintf("An error occurred when executing the \"%s\" command:\n\n%s\n\n%s.", escapeshellarg($cmd), $process->getOutput(), $process->getErrorOutput()));
47
        }
48
    }
49
50
    protected static function getPhpArguments()
51
    {
52
        $arguments = array();
53
54
        $phpFinder = new PhpExecutableFinder();
55
        if (method_exists($phpFinder, 'findArguments')) {
56
            $arguments = $phpFinder->findArguments();
57
        }
58
59
        if (false !== $ini = php_ini_loaded_file()) {
60
            $arguments[] = '--php-ini='.$ini;
61
        }
62
63
        return $arguments;
64
    }
65
}
66