<commit_before><?php

namespace Sly\NotificationPusher\Pusher;

use Sly\NotificationPusher\Pusher\BasePusherInterface;
use Sly\NotificationPusher\Collection\MessagesCollection;
use Sly\NotificationPusher\Model\MessageInterface;
use Sly\NotificationPusher\Exception\ConfigurationException;

/**
 * BasePusher.
 *
 * @uses BasePusherInterface
 * @author <NAME> <<EMAIL>>
 */
class BasePusher implements BasePusherInterface
{
    protected $config;
    protected $connection;
    protected $devicesUUIDs;
    protected $messages;

    /**
     * Constructor.
     *
     * @param array $config Configuration
     */
    public function __construct(array $config = array())
    {
        $this->config   = array_merge($this->getDefaultConfig(), $config);
        $this->messages = new MessagesCollection();

        if (empty($this->config['devices']) || null === $this->config['devices']) {
            throw new ConfigurationException('You must give an array of devices UUIDs to the pusher');
        }

        $this->devicesUUIDs = $this->config['devices'];
    }

    /**
     * Get default configuration.
     * 
     * @return array
     */
    protected function getDefaultConfig()
    {
        return array(
            'dev'      => false,
            'simulate' => false,
            'feedback' => false,
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * {@inheritdoc}
     */
    public function addMessage(MessageInterface $message)
    {
        $this->messages->set($message);

        return $this->messages;
    }

    /**
     * {@inheritdoc}
     */
    public function getMessages()
    {
        return $this->messages->getMessages();
    }

    /**
     * {@inheritdoc}
     */
    public function getConnection()
    {
        if (null === $this->connection) {
            $this->connection = $this->initAndGetConnection();
        }

        return $this->connection;
    }

    /**
     * {@inheritdoc}
     */
    public function getDevicesUUIDs()
    {
        return array_filter($this->devicesUUIDs);
    }

    /**
     * {@inheritdoc}
     */
    public function push()
    {
        $this->prePush();

        $this->connection = $this->initAndGetConnection();

        foreach ($this->getMessages() as $message) {
            if (true === $this->config['simulate']) {
                $message->setStatus(MessageInterface::STATUS_SIMULATED_SENT);
            }
            if (true === $this->pushMessage($message)) {
                $message->setStatus(MessageInterface::STATUS_SENT);
            } else {
                $message->setStatus(MessageInterface::STATUS_FAILED);
            }
        }

        $this->postPush();

        return $this->messages->getSentMessages();
    }

    /**
     * {@inheritdoc}
     */
    public function prePush()
    {
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function postPush()
    {
        return $this;
    }
}
<commit_msg>BasePusher devices tokens/IDs must be unique<commit_after><?php

namespace Sly\NotificationPusher\Pusher;

use Sly\NotificationPusher\Pusher\BasePusherInterface;
use Sly\NotificationPusher\Collection\MessagesCollection;
use Sly\NotificationPusher\Model\MessageInterface;
use Sly\NotificationPusher\Exception\ConfigurationException;

/**
 * BasePusher.
 *
 * @uses BasePusherInterface
 * @author <NAME> <<EMAIL>>
 */
class BasePusher implements BasePusherInterface
{
    protected $config;
    protected $connection;
    protected $devicesUUIDs;
    protected $messages;

    /**
     * Constructor.
     *
     * @param array $config Configuration
     */
    public function __construct(array $config = array())
    {
        $this->config   = array_merge($this->getDefaultConfig(), $config);
        $this->messages = new MessagesCollection();

        if (empty($this->config['devices']) || null === $this->config['devices']) {
            throw new ConfigurationException('You must give an array of devices UUIDs to the pusher');
        }

        $this->devicesUUIDs = $this->config['devices'];
    }

    /**
     * Get default configuration.
     * 
     * @return array
     */
    protected function getDefaultConfig()
    {
        return array(
            'dev'      => false,
            'simulate' => false,
            'feedback' => false,
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * {@inheritdoc}
     */
    public function addMessage(MessageInterface $message)
    {
        $this->messages->set($message);

        return $this->messages;
    }

    /**
     * {@inheritdoc}
     */
    public function getMessages()
    {
        return $this->messages->getMessages();
    }

    /**
     * {@inheritdoc}
     */
    public function getConnection()
    {
        if (null === $this->connection) {
            $this->connection = $this->initAndGetConnection();
        }

        return $this->connection;
    }

    /**
     * {@inheritdoc}
     */
    public function getDevicesUUIDs()
    {
        $devicesUUIDs     = array_filter($this->devicesUUIDs);
        $devicesContainer = array();

        foreach ($devicesUUIDs as $k => $dID) {
            if (true === is_array($dID) && false === in_array($dID[0], $devicesContainer)) {
                $devicesContainer[] = $dID[0];
            } elseif (true === is_string($dID) && false === in_array($dID, $devicesContainer)) {
                $devicesContainer[] = $dID;
            } else {
                unset($devicesUUIDs[$k]);
            }
        }

        return $devicesUUIDs;
    }

    /**
     * {@inheritdoc}
     */
    public function push()
    {
        $this->prePush();

        $this->connection = $this->initAndGetConnection();

        foreach ($this->getMessages() as $message) {
            if (true === $this->config['simulate']) {
                $message->setStatus(MessageInterface::STATUS_SIMULATED_SENT);
            }
            if (true === $this->pushMessage($message)) {
                $message->setStatus(MessageInterface::STATUS_SENT);
            } else {
                $message->setStatus(MessageInterface::STATUS_FAILED);
            }
        }

        $this->postPush();

        return $this->messages->getSentMessages();
    }

    /**
     * {@inheritdoc}
     */
    public function prePush()
    {
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function postPush()
    {
        return $this;
    }
}

{'fraction_non_alphanumeric': 0.1411443314615268, 'fraction_numerical': 0.00030353619669145547, 'mean_word_length': 2.032673722963645, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 76, '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': '3427400', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1859, 'n_words': 338}