Shell   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProcessExecutor() 0 12 3
A execute() 0 23 3
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches;
7
8
class Shell
9
{
10
    /**
11
     * @var \Vaimo\ComposerPatches\Logger
12
     */
13
    private $logger;
14
15
    /**
16
     * @var \Composer\Util\ProcessExecutor[]
17
     */
18
    private $processExecutors = array();
19
20
    /**
21
     * @param \Vaimo\ComposerPatches\Logger $logger
22
     */
23
    public function __construct(
24
        \Vaimo\ComposerPatches\Logger $logger
25
    ) {
26
        $this->logger = $logger;
27
    }
28
29
    /**
30
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
31
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
32
     *
33
     * @param string $command
34
     * @param null|string $cwd
35
     * @return array
36
     */
37
    public function execute($command, $cwd = null)
38
    {
39
        if (strpos($command, '<') === 0) {
40
            return array(true, trim($command, '< '));
41
        }
42
43
        $processExecutor = $this->getProcessExecutor();
44
        $logger = $this->logger;
45
46
        $output = '';
47
48
        $outputHandler = function ($type, $data) use ($logger, &$output) {
49
            $output .= $data;
50
            $logger->writeVerbose('comment', trim($data));
51
        };
52
53
        if ($this->logger->getOutputInstance()->isVerbose()) {
54
            $this->logger->writeIndentation();
55
        }
56
57
        $result = $processExecutor->execute($command, $outputHandler, $cwd);
58
59
        return array($result === 0, $output);
60
    }
61
62
    private function getProcessExecutor()
63
    {
64
        $output = $this->logger->getOutputInstance();
65
        $isMutedFlag = (int)$this->logger->isMuted();
66
67
        if (!isset($this->processExecutors[$isMutedFlag])) {
68
            $this->processExecutors[$isMutedFlag] = new \Composer\Util\ProcessExecutor(
69
                $isMutedFlag ? null : $output
70
            );
71
        }
72
73
        return $this->processExecutors[$isMutedFlag];
74
    }
75
}
76