<commit_before><?php

/*
 * This file is part of NotifyMe.
 *
 * (c) Alt Three LTD <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace NotifyMeHQ\Ballou;

use GuzzleHttp\Client;
use NotifyMeHQ\Contracts\GatewayInterface;
use NotifyMeHQ\NotifyMe\Arr;
use NotifyMeHQ\NotifyMe\HttpGatewayTrait;
use NotifyMeHQ\NotifyMe\Response;

class BallouGateway implements GatewayInterface
{
    use HttpGatewayTrait;

    /**
     * Gateway api endpoint.
     *
     * @var string
     */
    protected $endpoint = 'https://sms.ballou.se';

    /**
     * Ballou api version.
     *
     * @var string
     */
    protected $version = '1';

    /**
     * Create a new ballou gateway instance.
     *
     * @param \GuzzleHttp\Client $client
     * @param string[]           $config
     *
     * @return void
     */
    public function __construct(Client $client, array $config)
    {
        $this->client = $client;
        $this->config = $config;
    }

    /**
     * Send a notification.
     *
     * @param string $to
     * @param string $message
     *
     * @return \NotifyMeHQ\Contracts\ResponseInterface
     */
    public function notify($to, $message)
    {
        $params = [
            'UN'      => Arr::get($this->config, 'UN', ''),
            'PW'      => Arr::get($this->config, 'PW', ''),
            'CR'      => Arr::get($this->config, 'CR', ''),
            'RI'      => Arr::get($this->config, 'RI', ''),
            'O'       => urlencode(Arr::get($this->config, 'O', '')),
            'D'       => Arr::get($this->config, 'D', ''),
            'LONGSMS' => Arr::get($this->config, 'LONGSMS', ''),
            'M'       => urlencode($message),
        ];

        return $this->commit($this->buildUrlFromString('/http/get/SendSms.php'), $params);
    }

    /**
     * Commit a HTTP request.
     *
     * @param string   $url
     * @param string[] $params
     *
     * @return mixed
     */
    protected function commit($url, array $params)
    {
        $success = false;

        $rawResponse = $this->client->get($url, [
            'exceptions'      => false,
            'timeout'         => '80',
            'connect_timeout' => '30',
            'headers'         => [
                'Content-Type' => 'application/x-www-form-urlencoded',
            ],
            'body' => $params,
        ]);

        if ($rawResponse->getStatusCode() == 200) {
            $response = $rawResponse->xml();
            $success = (bool) $response->ballou_smls_response->response->message->attributes()->status;
        } else {
            $response = $this->responseError($rawResponse);
        }

        return $this->mapResponse($success, $response);
    }

    /**
     * Map HTTP response to response object.
     *
     * @param bool  $success
     * @param array $response
     *
     * @return \NotifyMeHQ\Contracts\ResponseInterface
     */
    protected function mapResponse($success, $response)
    {
        return (new Response())->setRaw($response)->map([
            'success' => $success,
            'message' => $success ? 'Message sent' : implode(', ', $response->ballou_smls_response->response->message->error),
        ]);
    }

    /**
     * Get the default json response.
     *
     * @param \GuzzleHttp\Message\ResponseInterface $rawResponse
     *
     * @return array
     */
    protected function jsonError($rawResponse)
    {
        $msg = 'API Response not valid.';
        $msg .= " (Raw response API {$rawResponse->getBody()})";

        return [
            'error' => $msg,<commit_msg>Cleanup<commit_after><?php

/*
 * This file is part of NotifyMe.
 *
 * (c) Alt Three LTD <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace NotifyMeHQ\Ballou;

use GuzzleHttp\Client;
use NotifyMeHQ\Contracts\GatewayInterface;
use NotifyMeHQ\NotifyMe\Arr;
use NotifyMeHQ\NotifyMe\HttpGatewayTrait;
use NotifyMeHQ\NotifyMe\Response;

class BallouGateway implements GatewayInterface
{
    use HttpGatewayTrait;

    /**
     * The api endpoint.
     *
     * @var string
     */
    protected $endpoint = 'https://sms.ballou.se';

    /**
     * The api version.
     *
     * @var string
     */
    protected $version = '1';

    /**
     * Create a new ballou gateway instance.
     *
     * @param \GuzzleHttp\Client $client
     * @param string[]           $config
     *
     * @return void
     */
    public function __construct(Client $client, array $config)
    {
        $this->client = $client;
        $this->config = $config;
    }

    /**
     * Send a notification.
     *
     * @param string $to
     * @param string $message
     *
     * @return \NotifyMeHQ\Contracts\ResponseInterface
     */
    public function notify($to, $message)
    {
        $params = [
            'UN'      => Arr::get($this->config, 'UN', ''),
            'PW'      => Arr::get($this->config, 'PW', ''),
            'CR'      => Arr::get($this->config, 'CR', ''),
            'RI'      => Arr::get($this->config, 'RI', ''),
            'O'       => urlencode(Arr::get($this->config, 'O', '')),
            'D'       => Arr::get($this->config, 'D', ''),
            'LONGSMS' => Arr::get($this->config, 'LONGSMS', ''),
            'M'       => urlencode($message),
        ];

        return $this->send($this->buildUrlFromString('/http/get/SendSms.php'), $params);
    }

    /**
     * Send the notification over the wire.
     *
     * @param string   $url
     * @param string[] $params
     *
     * @return \NotifyMeHQ\Contracts\ResponseInterface
     */
    protected function send($url, array $params)
    {
        $success = false;

        $rawResponse = $this->client->get($url, [
            'exceptions'      => false,
            'timeout'         => '80',
            'connect_timeout' => '30',
            'headers'         => [
                'Accept'       => 'application/xml',
                'Content-Type' => 'application/x-www-form-urlencoded',
            ],
            'body' => $params,
        ]);

        if ($rawResponse->getStatusCode() == 200) {
            $response = $rawResponse->xml();
            $success = (bool) $response->ballou_smls_response->response->message->attributes()->status;
        } else {
            $response = $this->responseError($rawResponse);
        }

        return $this->mapResponse($success, $response);
    }

    /**
     * Map the raw response to our response object.
     *
     * @param bool  $success
     * @param array $response
     *
     * @return \NotifyMeHQ\Contracts\ResponseInterface
     */
    protected function mapResponse($success, $response)
    {
        return (new Response())->setRaw($response)->map([
            'success' => $success,
            'message' => $success ? 'Message sent' : implode(', ', $response->ballou_smls_response->response->message->error),
        ]);
    }

    /**
     * Get the default json response.
     *
     * @param \GuzzleHttp\Message\ResponseInterface $rawResponse
     *
     * @return array
     */
    protected function jsonError($rawResponse)
    {
        $msg = 'API Response not valid.';
        $msg .= " (Raw response API {$rawResponse->getBody()})";

        return [
            'error' => $msg,
{'fraction_non_alphanumeric': 0.15616957876202087, 'fraction_numerical': 0.00216714072870107, 'mean_word_length': 1.9512390087929656, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 100, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4785018', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2193, 'n_words': 469}