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

namespace Plesk\MigrationLibV8\SiteImport\Entity\Status;

class RetrieveSiteInfo
{
    const OPERATION_CONNECT = 'connect';
    const OPERATION_SCAN_CONTENT = 'scan-content';

    const STATUS_NOT_STARTED = 'not-started';
    const STATUS_SUCCESS = 'success';
    const STATUS_FAILURE = 'failure';

    /** @var \string[] */
    private $_statuses;
    /** @var string|null */
    private $_errorMessage;
    /** @var string|null */
    private $_issueId;

    /**
     * RetrieveSiteInfo constructor.
     *
     * @param $statuses string[]
     * @param $errorMessage string|null
     * @param $issueId string|null
     */
    public function __construct($statuses, $errorMessage, $issueId)
    {
        $this->_statuses = $statuses;
        $this->_errorMessage = $errorMessage;
        $this->_issueId = $issueId;
    }

    /**
     * Get operation status - see STATUS_* constants.
     *
     * @param $operation string operation ID - see OPERATION_* constants
     * @return string see STATUS_* constants
     */
    public function getOperationStatus($operation)
    {
        return $this->_statuses[$operation];
    }

    /**
     * Whether specified operation was finished.
     *
     * @param $operation string operation ID - see OPERATION_* constants
     * @return bool
     */
    public function isOperationFinished($operation)
    {
        return $this->getOperationStatus($operation) != self::STATUS_NOT_STARTED;
    }

    /**
     * Whether specified operation was successful.
     *
     * @param $operation string operation ID - see OPERATION_* constants
     * @return bool
     */
    public function isOperationSuccessful($operation)
    {
        return $this->getOperationStatus($operation) == self::STATUS_SUCCESS;
    }

    /**
     * Get error message string if any of operations failed.
     *
     * @return string|null
     */
    public function getErrorMessage()
    {
        return $this->_errorMessage;
    }

    /**
     * Get backend issue ID if any of operations failed.
     *
     * @return string|null
     */
    public function getIssueId()
    {
        return $this->_issueId;
    }

    /**
     * Get retrieve site info status as array (useful for integration with JavaScript code).
     *
     * @return array
     */
    public function asArray()
    {
        return [
            'operations' => [
                self::OPERATION_CONNECT => $this->getOperationStatus(self::OPERATION_CONNECT),
                self::OPERATION_SCAN_CONTENT => $this->getOperationStatus(self::OPERATION_SCAN_CONTENT)
            ],
            'errorMessage' => $this->getErrorMessage(),
            'issueId' => $this->getIssueId()
        ];
    }
}
