Source of file Topics.php

Size: 2,209 Bytes - Last Modified: 2014-10-28T17:55:37+02:00

/Users/mcfedr/dev/awspushbundle/src/Mcfedr/AwsPushBundle/Service/Topics.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
<?php
namespace Mcfedr\AwsPushBundle\Service;

use Aws\Sns\Exception\SubscriptionLimitExceededException;
use Aws\Sns\SnsClient;
use Mcfedr\AwsPushBundle\Message\Message;
use Psr\Log\LoggerInterface;

/**
 * @deprecated Use the SnsClient directly to deal with topics
 * @see SnsClient
 */
class Topics
{

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

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

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

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

    /**
     * @param SnsClient $client
     * @param LoggerInterface $logger
     * @param Messages $messages
     * @param $debug
     */
    public function __construct(SnsClient $client, Messages $messages, $debug, LoggerInterface $logger = null)
    {
        $this->sns = $client;
        $this->logger = $logger;
        $this->messages = $messages;
        $this->debug = $debug;
    }

    /**
     * Subscribe a device to the topic, will create new numbered topics
     * once the first is full
     *
     * @param string $deviceArn
     * @param string $topicArn The base name of the topics to use
     * @throws SubscriptionLimitExceededException
     * @deprecated use SnsClient directly to subscribe
     * @see SnsClient::subscribe
     */
    public function registerDeviceOnTopic($deviceArn, $topicArn)
    {
        $this->sns->subscribe(
            [
                'TopicArn' => $topicArn,
                'Protocol' => 'application',
                'Endpoint' => $deviceArn
            ]
        );
    }

    /**
     * Send a message to all topics in the group
     *
     * @param Message $message
     * @param string $topicArn
     * @deprecated Use Messages send method and pass the topicArn as the destination
     * @see Messages::send
     */
    public function broadcast(Message $message, $topicArn)
    {
        if ($this->debug) {
            $this->logger && $this->logger->notice(
                "Message would have been sent to $topicArn",
                [
                    'Message' => $message
                ]
            );
            return;
        }

        $this->messages->send($message, $topicArn);
    }
}