<commit_before><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Narno\Gandi;

use Zend\Http\Client as HttpClient;
use Zend\XmlRpc;
use Zend\XmlRpc\Client\Exception\FaultException;
use Zend\XmlRpc\Client\Exception\HttpException;
use Narno\Gandi\Exception\RuntimeException;

/**
 * Class Api
 */
class Api
{
    /**
     * XML-RPC client.
     *
     * @var XmlRpc\Client
     */
    protected $xmlRpcClient;

    /**
     * Current method category (for method proxying).
     *
     * @var string|array
     */
    protected $methodCategory;

    /**
     * Categories of API methods.
     *
     * @var array
     */
    protected $methodCategories = [
        'account',
        'catalog',
        'cert',
        'changeowner',
        'contact',
        'datacenter',
        'disk',
        'domain',
        'hosting',
        'iface',
        'image',
        'ip',
        'operation',
        'paas',
        'product',
        'site',
        'system',
        'version',
        'vm',
    ];

    /**
     * Instantiates the XML-RPC client.
     *
     * @param bool $ote Set to true to use Operational Test and Evaluation
     */
    public function __construct($ote = false)
    {
        try {
            $this->xmlRpcClient = new XmlRpc\Client(null);
            $httpClient = new HttpClient($ote ? 'https://rpc.ote.gandi.net/xmlrpc/' : 'https://rpc.gandi.net/xmlrpc/', [
                'adapter'       => 'Zend\Http\Client\Adapter\Socket',
                'sslverifypeer' => false,
            ]);
            $this->xmlRpcClient->setHttpClient($httpClient);
            $this->xmlRpcClient->setSkipSystemLookup(true);
        } catch (FaultException $e) {
            throw new RuntimeException('Fault Exception: '.$e->getCode()."\n".$e->getMessage());
        } catch (HttpException $e) {
            throw new RuntimeException('HTTP Exception: '.$e->getCode()."\n".$e->getMessage());
        }
    }

    /**
     * Proxy service methods category.
     *
     * @param string $category
     *
     * @return self
     */
    public function __get($category)
    {
        $category = strtolower($category);
        $this->methodCategory[] = $category;

        return $this;
    }

    /**
     * Method overloading.
     *
     * @param string $method
     * @param array  $args
     *
     * @throws RuntimeException if unable to find method
     *
     * @return mixed
     */
    public function __call($method, $args)
    {
        $params = [];
        $apiMethod = '';

        $method = strtolower($method);
        if (!empty($args)) {
            $params = $args[0];
            if (!is_array($params)) {
                throw new RuntimeException(
                    '$params should be an array'
                );
            }
        }

        /*
         * If method category is not set
         */
        if (empty($this->methodCategory)) {
            throw new RuntimeException(
                'Invalid method "'.$method.'"'
            );
        }

        /*
         * Build method name: category1 + category2 + categoryX + method
         */
        foreach ($this->methodCategory as $methodCategory) {
            $apiMethod .= $methodCategory.'.';
        }
        $this->methodCategory = [];
        $method = $apiMethod.$method;

        /*
         * Request API directly
         */
        return $this->request($method, $params);
    }

    /**
     * Gandi API call.
     *
     * @param string $method
     * @param array  $params
     *
     * @return array
     */
    private function request($method, $params = [])
    {
        try {
            return $this->xmlRpcClient->call($method, $params);
        } catch (FaultException $e) {
            throw new RuntimeException('Fault Exception: '.$e->getCode()."\n".$e->getMessage());
        }
    }

    /**
     * Introspection.
     *
     * @todo useful or not?
     */

    /**
     * Available methods.
     *
     * @return array
     */
    public function listMethods()
    {
        return $this->request('system.listMethods');
    }

    /**
     * Method help.
     *
     * @param string $method Method name
     *
     * @return array
     */
    public function methodHelp($method)
    {
        return $this->request('system.methodHelp', [$method]);
    }

    /**
     * Method signature.
     *
     * @param string $method Method name
     *
     * @return array
     */
    public function methodSignature($method)
    {
        return $this->request('system.methodSignature', [$method]);
    }
}
<commit_msg>Applied fixes from StyleCI (#3)<commit_after><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Narno\Gandi;

use Narno\Gandi\Exception\RuntimeException;
use Zend\Http\Client as HttpClient;
use Zend\XmlRpc;
use Zend\XmlRpc\Client\Exception\FaultException;
use Zend\XmlRpc\Client\Exception\HttpException;

/**
 * Class Api.
 */
class Api
{
    /**
     * XML-RPC client.
     *
     * @var XmlRpc\Client
     */
    protected $xmlRpcClient;

    /**
     * Current method category (for method proxying).
     *
     * @var string|array
     */
    protected $methodCategory;

    /**
     * Categories of API methods.
     *
     * @var array
     */
    protected $methodCategories = [
        'account',
        'catalog',
        'cert',
        'changeowner',
        'contact',
        'datacenter',
        'disk',
        'domain',
        'hosting',
        'iface',
        'image',
        'ip',
        'operation',
        'paas',
        'product',
        'site',
        'system',
        'version',
        'vm',
    ];

    /**
     * Instantiates the XML-RPC client.
     *
     * @param bool $ote Set to true to use Operational Test and Evaluation
     */
    public function __construct($ote = false)
    {
        try {
            $this->xmlRpcClient = new XmlRpc\Client(null);
            $httpClient = new HttpClient($ote ? 'https://rpc.ote.gandi.net/xmlrpc/' : 'https://rpc.gandi.net/xmlrpc/', [
                'adapter'       => 'Zend\Http\Client\Adapter\Socket',
                'sslverifypeer' => false,
            ]);
            $this->xmlRpcClient->setHttpClient($httpClient);
            $this->xmlRpcClient->setSkipSystemLookup(true);
        } catch (FaultException $e) {
            throw new RuntimeException('Fault Exception: '.$e->getCode()."\n".$e->getMessage());
        } catch (HttpException $e) {
            throw new RuntimeException('HTTP Exception: '.$e->getCode()."\n".$e->getMessage());
        }
    }

    /**
     * Proxy service methods category.
     *
     * @param string $category
     *
     * @return self
     */
    public function __get($category)
    {
        $category = strtolower($category);
        $this->methodCategory[] = $category;

        return $this;
    }

    /**
     * Method overloading.
     *
     * @param string $method
     * @param array  $args
     *
     * @throws RuntimeException if unable to find method
     *
     * @return mixed
     */
    public function __call($method, $args)
    {
        $params = [];
        $apiMethod = '';

        $method = strtolower($method);
        if (!empty($args)) {
            $params = $args[0];
            if (!is_array($params)) {
                throw new RuntimeException(
                    '$params should be an array'
                );
            }
        }

        /*
         * If method category is not set
         */
        if (empty($this->methodCategory)) {
            throw new RuntimeException(
                'Invalid method "'.$method.'"'
            );
        }

        /*
         * Build method name: category1 + category2 + categoryX + method
         */
        foreach ($this->methodCategory as $methodCategory) {
            $apiMethod .= $methodCategory.'.';
        }
        $this->methodCategory = [];
        $method = $apiMethod.$method;

        /*
         * Request API directly
         */
        return $this->request($method, $params);
    }

    /**
     * Gandi API call.
     *
     * @param string $method
     * @param array  $params
     *
     * @return array
     */
    private function request($method, $params = [])
    {
        try {
            return $this->xmlRpcClient->call($method, $params);
        } catch (FaultException $e) {
            throw new RuntimeException('Fault Exception: '.$e->getCode()."\n".$e->getMessage());
        }
    }

    /**
     * Introspection.
     *
     * @todo useful or not?
     */

    /**
     * Available methods.
     *
     * @return array
     */
    public function listMethods()
    {
        return $this->request('system.listMethods');
    }

    /**
     * Method help.
     *
     * @param string $method Method name
     *
     * @return array
     */
    public function methodHelp($method)
    {
        return $this->request('system.methodHelp', [$method]);
    }

    /**
     * Method signature.
     *
     * @param string $method Method name
     *
     * @return array
     */
    public function methodSignature($method)
    {
        return $this->request('system.methodSignature', [$method]);
    }
}

{'fraction_non_alphanumeric': 0.13860752745495256, 'fraction_numerical': 0.0007463482247574368, 'mean_word_length': 1.6738882554161916, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '5468199', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2681, 'n_words': 604}