<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\Common\Exception;
/**
 * Exception which means that executed program failed with non-zero exit code
 */
class NonZeroExitCode extends \Exception
{
    /** @var string|null */
    private $_command;
    /** @var int|null */
    private $_exitCode;
    /** @var string|null */
    private $_stdout;
    /** @var string|null */
    private $_stderr;

    /**
     * Class constructor.
     * @param string|null $command command as a single string (optional)
     * @param int|null $exitCode exit code of the command (optional)
     * @param string|null $stdout stdout of the command (optional)
     * @param string|null $stderr stderr of the command (optional)
     */
    public function __construct($command=null, $exitCode=null, $stdout=null, $stderr=null)
    {
        $message = 'Command returned non-zero exit code.';

        if ($command !== null) {
            $message .= "\n\nCommand: $command";
        }
        if ($exitCode !== null) {
            $message .= "\n\nExit code: $exitCode";
        }
        if ($stdout !== null) {
            $message .= "\n\nstdout:\n$stdout";
        }
        if ($stderr !== null) {
            $message .= "\n\nstderr:\n$stderr";
        }

        parent::__construct($message);
    }

    /**
     * @return string|null
     */
    public function getCommand()
    {
        return $this->_command;
    }

    /**
     * @return int|null
     */
    public function getExitCode()
    {
        return $this->_exitCode;
    }

    /**
     * @return string|null
     */
    public function getStdout()
    {
        return $this->_stdout;
    }

    /**
     * @return string|null
     */
    public function getStderr()
    {
        return $this->_stderr;
    }
}
