Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

ScriptHandlerBase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 80
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 6 1
B executeCommand() 0 24 3
A getPhp() 0 11 2
1
<?php
2
/**
3
 * composer scripthandler base class
4
 */
5
6
namespace Graviton\CoreBundle\Composer;
7
8
use Symfony\Component\Process\Process;
9
use Symfony\Component\Process\PhpExecutableFinder;
10
use Composer\Script\CommandEvent;
11
12
/**
13
 * Base class for Composer ScriptHandlers
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
abstract class ScriptHandlerBase
0 ignored issues
show
Coding Style introduced by
ScriptHandlerBase does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
{
21
    /**
22
     * Composer variables are declared static so that an event could update
23
     * a composer.json and set new options, making them immediately available
24
     * to forthcoming listeners.
25
     */
26
    protected static $options = array(
27
        'symfony-app-dir' => 'app',
28
    );
29
30
    /**
31
     * Returns some options - adapted from Sensio DistributionBundle Command
32
     *
33
     * @param CommandEvent $event Event
34
     *
35
     * @return array Options
36
     */
37
    protected static function getOptions(CommandEvent $event)
38
    {
39
        $options = array_merge(self::$options, $event->getComposer()->getPackage()->getExtra());
40
        $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout');
41
        return $options;
42
    }
43
44
    /**
45
     * Executes a command
46
     *
47
     * @param CommandEvent $event      Event
48
     * @param string       $consoleDir Console dir
49
     * @param string       $cmd        Command
50
     * @param int          $timeout    Timeout
51
     *
52
     * @return void
53
     */
54
    protected static function executeCommand(CommandEvent $event, $consoleDir, $cmd, $timeout = 300)
55
    {
56
        $php = escapeshellarg(self::getPhp(false));
57
        $console = escapeshellarg($consoleDir.'/console');
58
        if ($event->getIO()->isDecorated()) {
59
            $console .= ' --ansi';
60
        }
61
62
        $process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout);
63
        $process->run(
64
            function ($type, $buffer) use ($event) {
65
                $event->getIO()->write($buffer, false);
66
            }
67
        );
68
69
        if (!$process->isSuccessful()) {
70
            throw new \RuntimeException(
71
                sprintf(
72
                    'An error occurred when executing the "%s" command.',
73
                    escapeshellarg($cmd)
74
                )
75
            );
76
        }
77
    }
78
79
80
    /**
81
     * Finds the path to the php executable
82
     *
83
     * @param bool $includeArgs include args
84
     *
85
     * @return bool|false|null|string path
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
86
     */
87
    protected static function getPhp($includeArgs = true)
88
    {
89
        $phpFinder = new PhpExecutableFinder();
90
        if (!$phpPath = $phpFinder->find($includeArgs)) {
91
            throw new \RuntimeException(
92
                'The php executable could not be found, add it to your PATH environment variable and try again'
93
            );
94
        }
95
96
        return $phpPath;
97
    }
98
}
99