vendor/redjanym/fcm-bundle/Entity/DeviceNotification.php line 9

Open in your IDE?
  1. <?php
  2. namespace RedjanYm\FCMBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use sngrl\PhpFirebaseCloudMessaging\Notification;
  5. use RedjanYm\FCMBundle\Model\DeviceNotificationInterface;
  6. class DeviceNotification extends Notification implements DeviceNotificationInterface
  7. {
  8.     use NotificationData;
  9.     /**
  10.      * @var ArrayCollection $deviceToken
  11.      */
  12.     private $deviceTokens;
  13.     public function __construct($title ''$body '')
  14.     {
  15.         parent::__construct($title$body);
  16.         $this->deviceTokens = new ArrayCollection();
  17.     }
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     public function setDeviceToken($token)
  22.     {
  23.         $this->deviceTokens[] = $token;
  24.         return $this;
  25.     }
  26.     
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function addDeviceToken($token)
  31.     {
  32.         if(is_string($token) === false){
  33.             throw new \InvalidArgumentException('Token must be a string!');
  34.         }
  35.         $this->deviceTokens->add($token);
  36.         return $this;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function setDeviceTokens($tokens)
  42.     {
  43.         if(is_array($tokens) === false){
  44.             throw new \InvalidArgumentException('Tokens must be an array of strings!');
  45.         }
  46.         $this->deviceTokens = new ArrayCollection($tokens);
  47.         return $this;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function getDeviceToken()
  53.     {
  54.         if ($this->deviceTokens->isEmpty() === false) {
  55.             return $this->deviceTokens->first();
  56.         } else {
  57.             throw new \InvalidArgumentException('The Mobile Notification must have a Device Token!');
  58.         }
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getDeviceTokens()
  64.     {
  65.         if ($this->deviceTokens->isEmpty() === false) {
  66.             return $this->deviceTokens->toArray();
  67.         } else {
  68.             throw new \InvalidArgumentException('The Mobile Notification must have a Device Token!');
  69.         }
  70.     }
  71. }