AbstractCommand.php 3.77 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Abstract shell command for the static code inspection
 */
namespace Magento\TestFramework\Inspection;

abstract class AbstractCommand
{
    /**
     * @var string
     */
    protected $_reportFile;

    /**
     * @var int
     */
    protected $_lastExitCode;

    /**
     * @var string
     */
    protected $_lastOutput;

    /**
     * @var string
     */
    protected $_lastRunMessage;

    /**
     * Constructor
     *
     * @param string $reportFile Destination file to write inspection report to
     */
    public function __construct($reportFile)
    {
        $this->_reportFile = $reportFile;
    }

    /**
     * Build and execute the shell command
     *
     * @param array $whiteList Files/directories to be inspected
     * @param array $blackList Files/directories to be excluded from the inspection
     * @return bool
     */
    public function run(array $whiteList, array $blackList = [])
    {
        if (file_exists($this->_reportFile)) {
            unlink($this->_reportFile);
        }
        $shellCmd = $this->_buildShellCmd($whiteList, $blackList);
        $result = $this->_execShellCmd($shellCmd);
        $this->_generateLastRunMessage();
        return $result !== false;
    }

    /**
     * Whether the command can be ran on the current environment
     *
     * @return bool
     */
    public function canRun()
    {
        return $this->_execShellCmd($this->_buildVersionShellCmd()) !== false;
    }

    /**
     * Retrieve the shell command version
     *
     * @return string|null
     */
    public function getVersion()
    {
        $versionOutput = $this->_execShellCmd($this->_buildVersionShellCmd());
        if (!$versionOutput) {
            return null;
        }
        return preg_match('/[^\d]*([^\s]+)/', $versionOutput, $matches) ? $matches[1] : $versionOutput;
    }

    /**
     * Get path to the report file
     *
     * @return string
     */
    public function getReportFile()
    {
        return $this->_reportFile;
    }

    /**
     * Build the shell command that outputs the version
     *
     * @return string
     */
    abstract protected function _buildVersionShellCmd();

    /**
     * Build the valid shell command
     *
     * @param array $whiteList
     * @param array $blackList
     * @return string
     */
    abstract protected function _buildShellCmd($whiteList, $blackList);

    /**
     * Execute a shell command on the current environment and return its output or FALSE on failure
     *
     * @param string $shellCmd
     * @return string|false
     */
    protected function _execShellCmd($shellCmd)
    {
        $output = [];
        exec($shellCmd . ' 2>&1', $output, $this->_lastExitCode);
        $this->_lastOutput = implode(PHP_EOL, $output);
        return $this->_lastExitCode === 0 ? $this->_lastOutput : false;
    }

    /**
     * Generate message about last execution result, prepared for output to a user
     *
     * @return \Magento\TestFramework\Inspection\AbstractCommand
     */
    protected function _generateLastRunMessage()
    {
        if ($this->_lastExitCode === null) {
            $this->_lastRunMessage = "Nothing was executed.";
        } elseif (!$this->_lastExitCode) {
            $this->_lastRunMessage = 'Success reported.';
        } elseif (file_exists($this->_reportFile)) {
            $this->_lastRunMessage = "See detailed report in '{$this->_reportFile}'.";
        } else {
            $this->_lastRunMessage = 'Command-line tool reports: ' . $this->_lastOutput;
        }
        return $this;
    }

    /**
     * Return message from the last run of the command
     *
     * @return string
     */
    public function getLastRunMessage()
    {
        return $this->_lastRunMessage;
    }
}