Passed
Push — master ( 92b8a3...77b4d2 )
by Bjørn
02:48
created

ExecTrait::hasNiceSupport()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.583

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 17
ccs 5
cts 7
cp 0.7143
crap 5.583
rs 9.6111
1
<?php
2
3
namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
6
7
/**
8
 * Trait for converters that uses exec()
9
 *
10
 * @package    WebPConvert
11
 * @author     Bjørn Rosell <[email protected]>
12
 * @since      Class available since Release 2.0.0
13
 */
14
trait ExecTrait
15
{
16
17
    abstract protected function logLn($msg, $style = '');
18
19
    /**
20
     * Helper function for examining if "nice" command is available
21
     *
22
     * @return  boolean  true if nice is available
23
     */
24 1
    protected static function hasNiceSupport()
25
    {
26 1
        exec("nice 2>&1", $niceOutput);
27
28 1
        if (is_array($niceOutput) && isset($niceOutput[0])) {
29 1
            if (preg_match('/usage/', $niceOutput[0]) || (preg_match('/^\d+$/', $niceOutput[0]))) {
30
                /*
31
                 * Nice is available - default niceness (+10)
32
                 * https://www.lifewire.com/uses-of-commands-nice-renice-2201087
33
                 * https://www.computerhope.com/unix/unice.htm
34
                 */
35
36 1
                return true;
37
            }
38
            return false;
39
        }
40
        return false; // to satisfy phpstan
41
    }
42
43
    /**
44
     * Logs output from the exec call.
45
     *
46
     * @param  array  $output
47
     *
48
     * @return  void
49
     */
50 2
    protected function logExecOutput($output)
51
    {
52 2
        if (is_array($output) && count($output) > 0) {
53
            $this->logLn('');
54
            $this->logLn('Output:', 'italic');
55
            foreach ($output as $line) {
56
                $this->logLn(print_r($line, true));
57
            }
58
            $this->logLn('');
59
        }
60 2
    }
61
62
    /**
63
     * Check basic operationality of exec converters (that the "exec" function is available)
64
     *
65
     * @throws  WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException
66
     * @return  void
67
     */
68 2
    public function checkOperationalityExecTrait()
69
    {
70 2
        if (!function_exists('exec')) {
71
            throw new SystemRequirementsNotMetException('exec() is not enabled.');
72
        }
73 2
    }
74
}
75