<commit_before><?php

/**
 * OwpClient package
 *
 * @package   Erliz\OwpClient
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) <NAME>
 * @license   MIT
 */

namespace Erliz\OwpClient;

use Erliz\OwpClient\Entity\HardwareServerEntity;
use Erliz\OwpClient\Entity\VirtualServerEntity;
use Erliz\OwpClient\Exception\ClientException;
use SimpleXMLElement;

/**
 * Class Client
 *
 * @author <NAME> <<EMAIL>>
 */
class Client
{
    /** @var string */
    private $host;
    /** @var string */
    private $port;
    /** @var string */
    private $user;
    /** @var string */
    private $pass;
    /** @var string */
    private $scheme;

    /**
     * Owp constructor.
     * @param string $host
     * @param string $user
     * @param string $pass
     * @param int    $port
     * @param string $scheme
     */
    public function __construct($host, $user = null, $pass = null, $port = 80, $scheme = 'http')
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->port = $port;
        $this->scheme = $scheme;
    }

    /**
     * @return array
     * @throws ClientException
     */
    public function getHardwareServers()
    {
        $hardwareServers = [];
        $response = $this->makeRequest('hardware_servers/list');
        foreach ($response->hardware_server as $node) {
            $hardwareServers[] = $this->generateHardwareServerEntity($node);
        }

        return $hardwareServers;
    }

    /**
     * @param int $id
     *
     * @return array
     * @throws ClientException
     */
    public function getVirtualServersByHardwareServerId($id)
    {
        $virtualServers = [];
        $response = $this->makeRequest(sprintf('hardware_servers/virtual_servers?id=%d', $id));
        foreach ($response->virtual_server as $node) {
            $virtualServers[] = $this->generateVirtualServerEntity($node);
        }

        return $virtualServers;
    }

    /**
     * @return VirtualServerEntity[]
     */
    public function getAllVirtualServers()
    {
        $virtualServers = [];
        /** @var HardwareServerEntity $hardwareServer */
        foreach ($this->getHardwareServers() as $hardwareServer) {
            $hwVtServers = $this->getVirtualServersByHardwareServerId($hardwareServer->getId());
            $virtualServers = array_merge($virtualServers, $hwVtServers);
        }

        return $virtualServers;
    }

    /**
     * @param string $method
     *
     * @return mixed
     * @throws ClientException
     */
    protected function makeRequest($method)
    {
        $curlHandler = curl_init();

        curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curlHandler, CURLOPT_VERBOSE, 0);
        curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curlHandler, CURLOPT_HTTPHEADER, []);
        curl_setopt($curlHandler, CURLOPT_HEADER, 0);
        curl_setopt($curlHandler, CURLOPT_POST, 0);
        curl_setopt($curlHandler, CURLOPT_URL, sprintf("%s://%s:%s/api/%s", $this->scheme, $this->host, $this->port, $method));
        if (!empty($this->user) && !empty($this->pass)) {
            curl_setopt($curlHandler, CURLOPT_USERPWD, sprintf('%s:%s', $this->user, $this->pass));
        }
        $result = curl_exec($curlHandler);
        $curlError = curl_error($curlHandler);

        if ($curlError) {
            throw new ClientException(sprintf('cURL Error: %s', $curlError));
        }
        curl_close($curlHandler);
        $doc = simplexml_load_string($result);

        return $doc;
    }

    /**
     * @param SimpleXMLElement $serverData
     *
     * @return HardwareServerEntity
     */
    private function generateHardwareServerEntity(SimpleXMLElement $serverData)
    {
        $server = new HardwareServerEntity();
        $server
            ->setDaemonPort((int) $serverData->daemon_port)
            ->setDefaultOSTemplate((string) $serverData->default_os_template)
            ->setDefaultServerTemplate((string) $serverData->default_server_template)
            ->setDescription((string) $serverData->description)
            ->setHost((string) $serverData->host)
            ->setId((int) $serverData->id)
            ->setUseSSL(filter_var((string) $serverData->use_ssl, FILTER_VALIDATE_BOOLEAN))
            ->setVSwap(filter_var((string) $serverData->vswap, FILTER_VALIDATE_BOOLEAN))
        ;

        return $server;
    }

    /**
     * @param SimpleXMLElement $serverData
     *
     * @return VirtualServerEntity
     */
    private function generateVirtualServerEntity(SimpleXMLElement $serverData)
    {
        $server = new VirtualServerEntity();
        $server
            ->setCpuLimit($this->isNilProperty($serverData->cpu_limit) ? false : $serverData->cpu_limit)
            ->setCpuUnits($this->isNilProperty($serverData->cpu_units) ? false : $serverData->cpu_units)
            ->setCpus($this->isNilProperty($serverData->cpus) ? false : $serverData->cpus)
            ->setDailyBackup(filter_var((string) $serverData->daily_backup, FILTER_VALIDATE_BOOLEAN))
            ->setDescription($serverData->description)
            ->setDiskSpace((int) $serverData->diskspace)
            ->setExpirationDate(
                $this->isNilProperty($serverData->expiration_date) ?
                    false :
                    date_create_from_format('Y-m-d|', $serverData->expiration_date)
            )<commit_msg>Fix hhvm exception, convert xml element to string<commit_after><?php

/**
 * OwpClient package
 *
 * @package   Erliz\OwpClient
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) <NAME>
 * @license   MIT
 */

namespace Erliz\OwpClient;

use Erliz\OwpClient\Entity\HardwareServerEntity;
use Erliz\OwpClient\Entity\VirtualServerEntity;
use Erliz\OwpClient\Exception\ClientException;
use SimpleXMLElement;

/**
 * Class Client
 *
 * @author <NAME> <<EMAIL>>
 */
class Client
{
    /** @var string */
    private $host;
    /** @var string */
    private $port;
    /** @var string */
    private $user;
    /** @var string */
    private $pass;
    /** @var string */
    private $scheme;

    /**
     * Owp constructor.
     * @param string $host
     * @param string $user
     * @param string $pass
     * @param int    $port
     * @param string $scheme
     */
    public function __construct($host, $user = null, $pass = null, $port = 80, $scheme = 'http')
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->port = $port;
        $this->scheme = $scheme;
    }

    /**
     * @return array
     * @throws ClientException
     */
    public function getHardwareServers()
    {
        $hardwareServers = [];
        $response = $this->makeRequest('hardware_servers/list');
        foreach ($response->hardware_server as $node) {
            $hardwareServers[] = $this->generateHardwareServerEntity($node);
        }

        return $hardwareServers;
    }

    /**
     * @param int $id
     *
     * @return array
     * @throws ClientException
     */
    public function getVirtualServersByHardwareServerId($id)
    {
        $virtualServers = [];
        $response = $this->makeRequest(sprintf('hardware_servers/virtual_servers?id=%d', $id));
        foreach ($response->virtual_server as $node) {
            $virtualServers[] = $this->generateVirtualServerEntity($node);
        }

        return $virtualServers;
    }

    /**
     * @return VirtualServerEntity[]
     */
    public function getAllVirtualServers()
    {
        $virtualServers = [];
        /** @var HardwareServerEntity $hardwareServer */
        foreach ($this->getHardwareServers() as $hardwareServer) {
            $hwVtServers = $this->getVirtualServersByHardwareServerId($hardwareServer->getId());
            $virtualServers = array_merge($virtualServers, $hwVtServers);
        }

        return $virtualServers;
    }

    /**
     * @param string $method
     *
     * @return mixed
     * @throws ClientException
     */
    protected function makeRequest($method)
    {
        $curlHandler = curl_init();

        curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curlHandler, CURLOPT_VERBOSE, 0);
        curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curlHandler, CURLOPT_HTTPHEADER, []);
        curl_setopt($curlHandler, CURLOPT_HEADER, 0);
        curl_setopt($curlHandler, CURLOPT_POST, 0);
        curl_setopt($curlHandler, CURLOPT_URL, sprintf("%s://%s:%s/api/%s", $this->scheme, $this->host, $this->port, $method));
        if (!empty($this->user) && !empty($this->pass)) {
            curl_setopt($curlHandler, CURLOPT_USERPWD, sprintf('%s:%s', $this->user, $this->pass));
        }
        $result = curl_exec($curlHandler);
        $curlError = curl_error($curlHandler);

        if ($curlError) {
            throw new ClientException(sprintf('cURL Error: %s', $curlError));
        }
        curl_close($curlHandler);
        $doc = simplexml_load_string($result);

        return $doc;
    }

    /**
     * @param SimpleXMLElement $serverData
     *
     * @return HardwareServerEntity
     */
    private function generateHardwareServerEntity(SimpleXMLElement $serverData)
    {
        $server = new HardwareServerEntity();
        $server
            ->setDaemonPort((int) $serverData->daemon_port)
            ->setDefaultOSTemplate((string) $serverData->default_os_template)
            ->setDefaultServerTemplate((string) $serverData->default_server_template)
            ->setDescription((string) $serverData->description)
            ->setHost((string) $serverData->host)
            ->setId((int) $serverData->id)
            ->setUseSSL(filter_var((string) $serverData->use_ssl, FILTER_VALIDATE_BOOLEAN))
            ->setVSwap(filter_var((string) $serverData->vswap, FILTER_VALIDATE_BOOLEAN))
        ;

        return $server;
    }

    /**
     * @param SimpleXMLElement $serverData
     *
     * @return VirtualServerEntity
     */
    private function generateVirtualServerEntity(SimpleXMLElement $serverData)
    {
        $server = new VirtualServerEntity();
        $server
            ->setCpuLimit($this->isNilProperty($serverData->cpu_limit) ? false : $serverData->cpu_limit)
            ->setCpuUnits($this->isNilProperty($serverData->cpu_units) ? false : $serverData->cpu_units)
            ->setCpus($this->isNilProperty($serverData->cpus) ? false : $serverData->cpus)
            ->setDailyBackup(filter_var((string) $serverData->daily_backup, FILTER_VALIDATE_BOOLEAN))
            ->setDescription($serverData->description)
            ->setDiskSpace((int) $serverData->diskspace)
            ->setExpirationDate(
                $this->isNilProperty($serverData->expiration_date) ?
                    false :
                    date_create_from_format('Y-m-d|', (string) $serverData->expiration_date)
            )
{'fraction_non_alphanumeric': 0.1364831776548472, 'fraction_numerical': 0.0012696109549288112, 'mean_word_length': 2.6145526057030484, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '100528', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3298, 'n_words': 566}