<commit_before><?php

namespace Synaq\ZasaBundle\Connector;

use InvalidArgumentException;
use \Synaq\ZasaBundle\Exception\DelegatedAuthDeniedException;
use Synaq\CurlBundle\Curl\Wrapper;
use Synaq\ZasaBundle\Exception\MissingConfigurationException;
use Synaq\ZasaBundle\Exception\RestErrorException;
use Synaq\ZasaBundle\Exception\SoapFaultException;
use Synaq\ZasaBundle\Util\Array2Xml;
use Synaq\ZasaBundle\Util\Xml2Array;

class ZimbraConnector
{
    /**
     * @var Wrapper
     */
    private $httpClient;

    /**
     * @var string
     */
    private $server;

    /**
     * @var string
     */
    private $adminUser;

    /**
     * @var string
     */
    private $adminPass;

    /**
     * @var string
     */
    private $authToken = false;

    /**
     * @var string
     */
    private $delegatedAuthToken = false;

    /**
     * @var string
     */
    private $delegatedAuthAccount = false;

    /**
     * @var bool
     */
    private $fopen = true;
    /**
     * @var string
     */
    private $sessionPath;

    /**
     * @var bool
     */
    private $login_init = false;
    /**
     * @var null
     */
    private $restServerBaseUrl;

    public function __construct(
        Wrapper $httpClient,
        $server,
        $adminUser,
        $adminPass,
        $fopen = true,
        $sessionPath = null,
        $restServerBaseUrl = null
    ) {
        $this->httpClient = $httpClient;
        $this->server = $server;
        $this->adminUser = $adminUser;
        $this->adminPass = $<PASSWORD>;
        $this->fopen = $fopen;
        $this->sessionPath = $sessionPath;
        $this->restServerBaseUrl = $restServerBaseUrl;

        if (!empty($this->sessionPath) && file_exists($this->sessionPath)) {
            $this->authToken = file_get_contents($this->sessionPath);
        }
    }

    /**
     * @param $requestType
     * @param array $attributes
     * @param array $parameters
     * @param false $delegate
     * @param string $delegateType
     * @param bool $retryOnExpiredAuth
     * @return mixed
     * @throws SoapFaultException
     */
    private function request(
        $requestType,
        $attributes = array(),
        $parameters = array(),
        $delegate = false,
        $delegateType = 'Mail',
        $retryOnExpiredAuth = true
    ) {
        try {
            if (!$this->login_init && !$this->authToken) {
                $this->login();
            }
            $request = $this->buildRequest($requestType, $attributes, $parameters, $delegate, $delegateType);
            $response = $this->submitRequest($request);
            $response = $response['soap:Envelope']['soap:Body'][$requestType.'Response'];
        } catch (SoapFaultException $e) {
            if ($e->getMessage() == 'Zimbra Soap Fault: auth credentials have expired' && $retryOnExpiredAuth) {
                $this->login();
                $response = $this->request($requestType, $attributes, $parameters, $delegate, $delegateType, false);
            } else {

                throw $e;
            }
        }

        return $response;
    }

    /**
     * @param $requestType
     * @param $attributes
     * @param $parameters
     * @param $delegate<commit_msg>Retries delegation if login cretendials expired<commit_after><?php

namespace Synaq\ZasaBundle\Connector;

use InvalidArgumentException;
use \Synaq\ZasaBundle\Exception\DelegatedAuthDeniedException;
use Synaq\CurlBundle\Curl\Wrapper;
use Synaq\ZasaBundle\Exception\MissingConfigurationException;
use Synaq\ZasaBundle\Exception\RestErrorException;
use Synaq\ZasaBundle\Exception\SoapFaultException;
use Synaq\ZasaBundle\Util\Array2Xml;
use Synaq\ZasaBundle\Util\Xml2Array;

class ZimbraConnector
{
    /**
     * @var Wrapper
     */
    private $httpClient;

    /**
     * @var string
     */
    private $server;

    /**
     * @var string
     */
    private $adminUser;

    /**
     * @var string
     */
    private $adminPass;

    /**
     * @var string
     */
    private $authToken = false;

    /**
     * @var string
     */
    private $delegatedAuthToken = false;

    /**
     * @var string
     */
    private $delegatedAuthAccount = false;

    /**
     * @var bool
     */
    private $fopen = true;
    /**
     * @var string
     */
    private $sessionPath;

    /**
     * @var bool
     */
    private $login_init = false;
    /**
     * @var null
     */
    private $restServerBaseUrl;

    public function __construct(
        Wrapper $httpClient,
        $server,
        $adminUser,
        $adminPass,
        $fopen = true,
        $sessionPath = null,
        $restServerBaseUrl = null
    ) {
        $this->httpClient = $httpClient;
        $this->server = $server;
        $this->adminUser = $adminUser;
        $this->adminPass = $<PASSWORD>;
        $this->fopen = $fopen;
        $this->sessionPath = $sessionPath;
        $this->restServerBaseUrl = $restServerBaseUrl;

        if (!empty($this->sessionPath) && file_exists($this->sessionPath)) {
            $this->authToken = file_get_contents($this->sessionPath);
        }
    }

    /**
     * @param $requestType
     * @param array $attributes
     * @param array $parameters
     * @param false $delegate
     * @param string $delegateType
     * @param bool $retryOnExpiredAuth
     * @return mixed
     * @throws SoapFaultException
     */
    private function request(
        $requestType,
        $attributes = array(),
        $parameters = array(),
        $delegate = false,
        $delegateType = 'Mail',
        $retryOnExpiredAuth = true
    ) {
        try {
            if (!$this->login_init && !$this->authToken) {
                $this->login();
            }
            $request = $this->buildRequest($requestType, $attributes, $parameters, $delegate, $delegateType);
            $response = $this->submitRequest($request);
            $response = $response['soap:Envelope']['soap:Body'][$requestType.'Response'];
        } catch (SoapFaultException $e) {
            if ($e->getMessage() == 'Zimbra Soap Fault: auth credentials have expired' && $retryOnExpiredAuth) {
                $this->login();

                if ($delegate) {
                    $authAccount = $this->delegatedAuthAccount;
                    $this->delegatedAuthAccount = false;
                    $this->delegateAuth($authAccount);
                }

                $response = $this->request($requestType, $attributes, $parameters, $delegate, $delegateType, false);
            } else {

                throw $e;
            }
        }

        return $response;
    }

    /**
     * @param $requestType
     * @param $attributes
     * @param $parameters
     * @param $delegate
{'fraction_non_alphanumeric': 0.12946095266537255, 'fraction_numerical': 0.0005972823652381663, 'mean_word_length': 2.0445454545454544, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2618922', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1940, 'n_words': 385}