<commit_before><?php

/**
 * This file is part of the SmsSender package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

namespace SmsSender;

use SmsSender\Provider\ProviderInterface;
use SmsSender\Result\Sms;

/**
 * @author <NAME> <<EMAIL>>
 */
class SmsSender implements SmsSenderInterface
{
    /**
     * Version
     */
    const VERSION = '1.0.0';

    /**
     * @var array
     */
    protected $providers = array();

    /**
     * @var \SmsSender\Provider\ProviderInterface
     */
    protected $provider = null;

    /**
     * @param \SmsSender\Provider\ProviderInterface $provider
     */
    public function __construct(ProviderInterface $provider = null)
    {
        $this->provider = $provider;
    }

    /**
     * {@inheritDoc}
     */
    public function send($recipient, $body, $originator = '')
    {
        if (empty($recipient) || empty($body)) {
            // let's save a request
            return $this->returnResult(array());
        }

        $data   = $this->getProvider()->send($recipient, $body, $originator);
        $result = $this->returnResult($data);

        return $result;
    }

    /**
     * Registers a provider.
     *
     * @param  \SmsSender\Provider\ProviderInterface $provider
     * @return \SmsSender\AbstractProvider
     */
    public function registerProvider(ProviderInterface $provider)
    {
        if (null !== $provider) {
            $this->providers[$provider->getName()] = $provider;
        }

        return $this;
    }

    /**
     * Registers a set of providers.
     *
     * @param  array                       $providers
     * @return \SmsSender\AbstractProvider
     */
    public function registerProviders(array $providers = array())
    {
        foreach ($providers as $provider) {
            $this->registerProvider($provider);
        }

        return $this;
    }

    /**
     * Sets the provider to use.
     *
     * @param  string                      $name A provider's name
     * @return \SmsSender\AbstractProvider
     */
    public function using($name)
    {
        if (isset($this->providers[$name])) {
            $this->provider = $this->providers[$name];
        }

        return $this;
    }

    /**
     * Returns registered providers indexed by name.
     *
     * @return array
     */
    public function getProviders()
    {
        return $this->providers;
    }

    /**
     * Returns the provider to use.
     *
     * @return \SmsSender\Provider\ProviderInterface
     */
    protected function getProvider()
    {
        if (null === $this->provider) {
            if (0 === count($this->providers)) {
                throw new \RuntimeException('No provider registered.');
            } else {
                $this->provider = $this->providers[key($this->providers)];
            }
        }

        return $this->provider;
    }

    /**
     * @param  array                 $data An array of data.
     * @return \SmsSender\Result\Sms
     */
    protected function returnResult(array $data = array())
    {
        $result = new Sms();
        $result->fromArray($data);

        return $result;
    }
}

// vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent:
<commit_msg>Fix: return a FAILED status when no recipient or body is given to the sender<commit_after><?php

/**
 * This file is part of the SmsSender package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

namespace SmsSender;

use SmsSender\Provider\ProviderInterface;
use SmsSender\Result\ResultInterface;
use SmsSender\Result\Sms;

/**
 * @author <NAME> <<EMAIL>>
 */
class SmsSender implements SmsSenderInterface
{
    /**
     * Version
     */
    const VERSION = '1.0.0';

    /**
     * @var array
     */
    protected $providers = array();

    /**
     * @var \SmsSender\Provider\ProviderInterface
     */
    protected $provider = null;

    /**
     * @param \SmsSender\Provider\ProviderInterface $provider
     */
    public function __construct(ProviderInterface $provider = null)
    {
        $this->provider = $provider;
    }

    /**
     * {@inheritDoc}
     */
    public function send($recipient, $body, $originator = '')
    {
        if (empty($recipient) || empty($body)) {
            // let's save a request
            return $this->returnResult(array(
                'status' => ResultInterface::STATUS_FAILED,
            ));
        }

        $data   = $this->getProvider()->send($recipient, $body, $originator);
        $result = $this->returnResult($data);

        return $result;
    }

    /**
     * Registers a provider.
     *
     * @param  \SmsSender\Provider\ProviderInterface $provider
     * @return \SmsSender\AbstractProvider
     */
    public function registerProvider(ProviderInterface $provider)
    {
        if (null !== $provider) {
            $this->providers[$provider->getName()] = $provider;
        }

        return $this;
    }

    /**
     * Registers a set of providers.
     *
     * @param  array                       $providers
     * @return \SmsSender\AbstractProvider
     */
    public function registerProviders(array $providers = array())
    {
        foreach ($providers as $provider) {
            $this->registerProvider($provider);
        }

        return $this;
    }

    /**
     * Sets the provider to use.
     *
     * @param  string                      $name A provider's name
     * @return \SmsSender\AbstractProvider
     */
    public function using($name)
    {
        if (isset($this->providers[$name])) {
            $this->provider = $this->providers[$name];
        }

        return $this;
    }

    /**
     * Returns registered providers indexed by name.
     *
     * @return array
     */
    public function getProviders()
    {
        return $this->providers;
    }

    /**
     * Returns the provider to use.
     *
     * @return \SmsSender\Provider\ProviderInterface
     */
    protected function getProvider()
    {
        if (null === $this->provider) {
            if (0 === count($this->providers)) {
                throw new \RuntimeException('No provider registered.');
            } else {
                $this->provider = $this->providers[key($this->providers)];
            }
        }

        return $this->provider;
    }

    /**
     * @param  array                 $data An array of data.
     * @return \SmsSender\Result\Sms
     */
    protected function returnResult(array $data = array())
    {
        $result = new Sms();
        $result->fromArray($data);

        return $result;
    }
}

// vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent:

{'fraction_non_alphanumeric': 0.12804341449105308, 'fraction_numerical': 0.002053388090349076, 'mean_word_length': 1.9029374201787994, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, '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': '5420535', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1897, 'n_words': 456}