<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap;


use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\XmlParser;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 05.09.19
 * Time: 10:51
 * Class Response
 */
class Response
{
    protected $request;
    protected $options;
    protected $params;
    protected $headers;
    protected $xmlResponse;
    protected $responseHead;
    protected $responseBody;
    protected $responseModel;
    protected $lastError;
    protected $errors = [];

    /**
     * @return mixed
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * @param $request
     * @return $this
     */
    public function setRequest($request)
    {
        $this->request = $request;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * @param $options
     * @return $this
     */
    public function setOptions($options)
    {
        $this->options = $options;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getParams()
    {
        return $this->params;
    }

    /**
     * @param $params
     * @return $this
     */
    public function setParams($params)
    {
        $this->params = $params;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getHeaders()
    {
        return $this->headers;
    }

    /**
     * @param $headers
     * @return $this
     */
    public function setHeaders($headers)
    {
        $this->headers = $headers;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getXmlResponse()
    {
        return $this->xmlResponse;
    }

    /**
     * @param $xmlResponse
     * @return $this
     */
    public function setXmlResponse($xmlResponse)
    {
        $this->xmlResponse = $xmlResponse;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getResponseBody()
    {
        return $this->responseBody;
    }

    /**
     * @return mixed
     */
    public function getResponseModel()
    {
        return $this->responseModel;
    }

    /**
     * @param $responseModel
     * @return $this
     */
    public function setResponseModel($responseModel)
    {
        $this->responseModel = $responseModel;
        return $this;
    }

    /**
     *
     */
    public function getLastError()
    {
        return $this->lastError;
    }

    /**
     * @param $lastError
     */
    public function setLastError($lastError)
    {
        //todo save to file

        $this->errors[] = $lastError;
        $this->lastError = $lastError;
    }

    /**
     * @return bool
     */
    public function getLastErrorCode()
    {
        if($code = $this->responseBody['SOAP:FAULT']['SOAP:DETAIL']['ERROR']['CODE']['DATA'])
        {
            return $code;
        }

        return false;
    }

    /**
     * Parse response
     *
     * @return $this
     */
    public function response()
    {

        /**
         * xml parse response
         */
        $xml = new XmlParser();
        $array = $xml->parse($this->xmlResponse);

        /**
         * set response header
         */
        $this->responseHead = $array['SOAP:ENVELOPE']['SOAP:HEADER'];

        /**
         * set response body
         */
        $this->responseBody = $array['SOAP:ENVELOPE']['SOAP:BODY'];

        return $this;

    }

}