src/EventSubscriber/NotificationSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Notification;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Nyholm\Psr7\Factory\Psr17Factory;
  8. use OneSignal\Config;
  9. use OneSignal\OneSignal;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpClient\Psr18Client;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  16. class NotificationSubscriber implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private $config;
  20.     private $httpClient;
  21.     private $requestFactory;
  22.     private $oneSignal;
  23.     private $streamFactory;
  24.     public function __construct(EntityManagerInterface $entityManager)
  25.     {
  26.         $this->entityManager $entityManager;
  27.         $this->config = new Config('26b2a12d-5cb9-4eea-a668-01f7e98ad9c2''MjNjZjcwNDItNzkyNC00MjBlLTg4NGMtZWZkNTNjNzkzMDU1');
  28.         $this->httpClient = new Psr18Client();
  29.         $this->requestFactory $this->streamFactory = new Psr17Factory();
  30.         $this->oneSignal = new OneSignal($this->config$this->httpClient$this->requestFactory$this->streamFactory);
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             KernelEvents::VIEW => ['sendNotification'EventPriorities::PRE_WRITE],
  36.         ];
  37.     }
  38.     public function sendNotification(ViewEvent $event): void
  39.     {
  40.         $notification $event->getControllerResult();
  41.         $method $event->getRequest()->getMethod();
  42.         if (!$notification instanceof Notification || Request::METHOD_POST !== $method) {
  43.             return;
  44.         }
  45.         /*hashes_array = array();
  46.         $hashes_array[] = array(
  47.             "id" => "like-button",
  48.             "text" => "Like",
  49.             "icon" => "http://i.imgur.com/N8SN8ZS.png",
  50.             "url" => "https://yoursite.com"
  51.         );
  52.         $hashes_array[] = array(
  53.             "id" => "like-button-2",
  54.             "text" => "Like2",
  55.             "icon" => "http://i.imgur.com/N8SN8ZS.png",
  56.             "url" => "https://yoursite.com"
  57.         );*/
  58.         if(is_null($notification->getDeliverAt())){
  59.             $this->oneSignal->notifications()->add([
  60.                 'contents' => [
  61.                     $notification->getLocale() => $notification->getMessage()
  62.                 ],
  63.                 'subtitle' => [
  64.                     'en' => $notification->getSubtitle()
  65.                 ],
  66.                 'included_segments' => ['Subscribed Users'],
  67.                 'data' => ["target" => $notification->getTarget(),"id" => $notification->getIdobject()]
  68.             ]);
  69.         } else {
  70.             $this->oneSignal->notifications()->add([
  71.                 'contents' => [
  72.                     $notification->getLocale() => $notification->getMessage()
  73.                 ],
  74.                 'subtitle' => [
  75.                     'en' => $notification->getSubtitle()
  76.                 ],
  77.                 'included_segments' => ['Subscribed Users'],
  78.                 'data' => ["target" => $notification->getTarget(),"id" => $notification->getIdobject()],
  79.                 'send_after' => new \DateTimeImmutable($notification->getDeliverAt())
  80.             ]);
  81.         }
  82.     }
  83. }