ExecCliStep   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 57
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 46 7
1
<?php
2
3
/**
4
 * AppserverIo\Provisioning\Steps\ExecCliStep
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/provisioning
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Provisioning\Steps;
22
23
/**
24
 * An step implementation that executes a PHP script defined in configuration.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2018 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/provisioning
30
 * @link      http://www.appserver.io
31
 */
32
class ExecCliStep extends AbstractStep
33
{
34
35
    /**
36
     * Executes the functionality for this step, in this case the execution of
37
     * the PHP script defined in the step configuration.
38
     *
39
     * @return void
40
     * @throws \Exception Is thrown if the script can't be executed
41
     * @see \AppserverIo\Provisioning\Steps\StepInterface::execute()
42
     */
43
    public function execute()
44
    {
45
46
        // try to load the script from the configuration
47
        if ($script = $this->getStepNode()->getExecute()->getScript()) {
0 ignored issues
show
Bug introduced by
The method getScript() does not exist on AppserverIo\Provisioning\Api\Node\InstallationNode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        if ($script = $this->getStepNode()->getExecute()->/** @scrutinizer ignore-call */ getScript()) {

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...
48
            // prepare script by prepending the webapp directory
49
            $script = new \SplFileInfo(
50
                $this->getWebappPath() . DIRECTORY_SEPARATOR . ltrim($script, DIRECTORY_SEPARATOR)
51
            );
52
53
            // check if the configured script is a file
54
            if ($script->isFile() === false) {
55
                throw new \Exception(sprintf('Script %s is not a file', $script));
56
            }
57
58
            // prepare the scripts arguments
59
            $args = '';
60
            if ($params = $this->getStepNode()->getExecute()->getArgs()) {
0 ignored issues
show
Bug introduced by
The method getArgs() does not exist on AppserverIo\Provisioning\Api\Node\InstallationNode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            if ($params = $this->getStepNode()->getExecute()->/** @scrutinizer ignore-call */ getArgs()) {

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...
61
                $args .= ' -- ';
62
                foreach ($params as $param) {
63
                    // query whether or not the argument has a name
64
                    if ($name = $param->getName()) {
65
                        $args .= ' --' . $name;
66
                    }
67
                    // append the value finally
68
                    $args .= ' ' . $param->castToType();
69
                }
70
            }
71
72
            // prepare the PHP executable, the script and the arguments
73
            $toExecute = $this->getPhpExecutable() . ' -f ' . $script . $args;
74
75
            // initialize exec() output and return var
76
            $output = array();
77
            $returnVar = 0;
78
79
80
            $this->getInitialContext()->getSystemLogger()->info("Now execute: $toExecute");
81
82
            // execute the script on the command line
83
            exec($toExecute, $output, $returnVar);
84
85
            // check if script has been executed successfully
86
            if ($returnVar !== 0) {
87
                // if not, throw an exception
88
                throw new \Exception(implode(PHP_EOL, $output));
89
            }
90
        }
91
    }
92
}
93