Source of file Messages.php
Size: 3,977 Bytes - Last Modified: 2014-10-15T13:48:55+03:00
/Users/mcfedr/dev/awspushbundle/src/Mcfedr/AwsPushBundle/Service/Messages.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | <?php namespace Mcfedr\AwsPushBundle\Service; use Aws\Sns\SnsClient; use Mcfedr\AwsPushBundle\Exception\PlatformNotConfiguredException; use Mcfedr\AwsPushBundle\Message\Message; use Psr\Log\LoggerInterface; class Messages { /** * @var SnsClient */ private $sns; /** * @var array */ private $arns; /** * @var LoggerInterface */ private $logger; /** * @var bool */ private $debug; /** * @param SnsClient $client * @param array $platformARNS * @param LoggerInterface $logger * @param bool $debug */ public function __construct(SnsClient $client, $platformARNS, $debug, LoggerInterface $logger = null) { $this->sns = $client; $this->arns = $platformARNS; $this->logger = $logger; $this->debug = $debug; } /** * Send a message to all devices on one or all platforms * * @param Message $message * @param string $platform * @throws \Mcfedr\AwsPushBundle\Exception\PlatformNotConfiguredException */ public function broadcast(Message $message, $platform = null) { if ($platform != null && !isset($this->arns[$platform])) { throw new PlatformNotConfiguredException("There is no configured ARN for $platform"); } $messageData = json_encode($message, JSON_UNESCAPED_UNICODE); if ($platform) { $this->broadcastToPlatform($messageData, $platform); } else { foreach ($this->arns as $platform => $arn) { $this->broadcastToPlatform($messageData, $platform); } } } /** * Send a message to an endpoint * * @param Message|string $message * @param string $endpointArn */ public function send($message, $endpointArn) { if ($this->debug) { $this->logger && $this->logger->notice( "Message would have been sent to $endpointArn", [ 'Message' => $message ] ); return; } $this->sns->publish( [ 'TargetArn' => $endpointArn, 'Message' => $message instanceof Message ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message, 'MessageStructure' => 'json' ] ); } /** * Send a message to all devices on a platform * * @param Message|string $message * @param string $platform */ private function broadcastToPlatform($message, $platform) { if ($this->debug) { $this->logger && $this->logger->notice( "Message would have been sent to $platform", [ 'Message' => $message ] ); return; } foreach ($this->sns->getListEndpointsByPlatformApplicationIterator( [ 'PlatformApplicationArn' => $this->arns[$platform] ] ) as $endpoint) { if ($endpoint['Attributes']['Enabled'] == "true") { try { $this->send($message, $endpoint['EndpointArn']); } catch (\Exception $e) { $this->logger && $this->logger->error( "Failed to push to {$endpoint['EndpointArn']}", [ 'Message' => $message, 'Exception' => $e, 'Endpoint' => $endpoint ] ); } } else { $this->logger && $this->logger->info( "Disabled endpoint {$endpoint['EndpointArn']}", [ 'Message' => $message, 'Endpoint' => $endpoint ] ); } } } } |