<commit_before><?php
namespace mcfedr\AWSPushBundle\Service;

use Aws\Sns\Exception\SubscriptionLimitExceededException;
use Aws\Sns\Exception\TopicLimitExceededException;
use Aws\Sns\SnsClient;
use Doctrine\Common\Cache\Cache;
use mcfedr\AWSPushBundle\Message\Message;
use mcfedr\AWSPushBundle\Topic\Topic;
use Psr\Log\LoggerInterface;

class Topics
{

    /**
     * @var SnsClient
     */
    private $sns;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var Messages
     */
    private $messages;

    /**
     * @var Cache
     */
    protected $cache;

    /**
     * @var bool
     */
    private $debug;

    /**
     * @param SnsClient $client
     * @param LoggerInterface $logger
     * @param Messages $messages
     * @param \Doctrine\Common\Cache\Cache $cache
     * @param $debug
     * @internal param $cacheDir
     */
    public function __construct(SnsClient $client, LoggerInterface $logger, Messages $messages, Cache $cache, $debug)
    {
        $this->sns = $client;
        $this->logger = $logger;
        $this->messages = $messages;
        $this->cache = $cache;
        $this->debug = $debug;
    }

    /**
     * Subscribe a device to the topic, will create new numbered topics
     * once the first is full
     *
     * @param string $deviceArn
     * @param string $topicName The base name of the topics to use
     * @throws TopicLimitExceededException
     */
    public function registerDeviceOnTopic($deviceArn, $topicName)
    {
        $lastTopic = null;
        if (!$this->iterateTopics(
            $topicName,
            function (Topic $topic) use ($deviceArn, &$lastTopic) {
                $lastTopic = $topic;
                try {
                    $this->registerDevice($deviceArn, $topic);
                    return true;
                } catch (SubscriptionLimitExceededException $e) {
                    return false;
                }
            }
        )
        ) {
            $this->registerDevice(
                $deviceArn,
                $this->createNextTopic($lastTopic->getName(), $lastTopic->getNumber() + 1)
            );
        }
    }

    /**
     * Send a message to all topics in the group
     *
     * @param Message $message
     * @param string $topicName
     */
    public function broadcast(Message $message, $topicName)
    {
        if ($this->debug) {
            $this->logger->notice(<commit_msg>Fix bug creating first topic<commit_after><?php
namespace mcfedr\AWSPushBundle\Service;

use Aws\Sns\Exception\SubscriptionLimitExceededException;
use Aws\Sns\Exception\TopicLimitExceededException;
use Aws\Sns\SnsClient;
use Doctrine\Common\Cache\Cache;
use mcfedr\AWSPushBundle\Message\Message;
use mcfedr\AWSPushBundle\Topic\Topic;
use Psr\Log\LoggerInterface;

class Topics
{

    /**
     * @var SnsClient
     */
    private $sns;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var Messages
     */
    private $messages;

    /**
     * @var Cache
     */
    protected $cache;

    /**
     * @var bool
     */
    private $debug;

    /**
     * @param SnsClient $client
     * @param LoggerInterface $logger
     * @param Messages $messages
     * @param \Doctrine\Common\Cache\Cache $cache
     * @param $debug
     * @internal param $cacheDir
     */
    public function __construct(SnsClient $client, LoggerInterface $logger, Messages $messages, Cache $cache, $debug)
    {
        $this->sns = $client;
        $this->logger = $logger;
        $this->messages = $messages;
        $this->cache = $cache;
        $this->debug = $debug;
    }

    /**
     * Subscribe a device to the topic, will create new numbered topics
     * once the first is full
     *
     * @param string $deviceArn
     * @param string $topicName The base name of the topics to use
     * @throws TopicLimitExceededException
     */
    public function registerDeviceOnTopic($deviceArn, $topicName)
    {
        $lastTopic = null;
        if (!$this->iterateTopics(
            $topicName,
            function (Topic $topic) use ($deviceArn, &$lastTopic) {
                $lastTopic = $topic;
                try {
                    $this->registerDevice($deviceArn, $topic);
                    return true;
                } catch (SubscriptionLimitExceededException $e) {
                    return false;
                }
            }
        )
        ) {
            $this->registerDevice(
                $deviceArn,
                $this->createNextTopic($topicName, $lastTopic ? $lastTopic->getNumber() + 1 : 0)
            );
        }
    }

    /**
     * Send a message to all topics in the group
     *
     * @param Message $message
     * @param string $topicName
     */
    public function broadcast(Message $message, $topicName)
    {
        if ($this->debug) {
            $this->logger->notice(
{'fraction_non_alphanumeric': 0.11384615384615385, 'fraction_numerical': 0.0006153846153846154, 'mean_word_length': 1.9023809523809523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '664079', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1397, 'n_words': 333}