← Back
Editing: NotificationService.php
<?php namespace App\Service; use App\Entity\AddressReminder; use App\Entity\Adresse; use App\Entity\UserAddress; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\EntityManagerInterface; use Nyholm\Psr7\Factory\Psr17Factory; use OneSignal\Config; use OneSignal\OneSignal; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Psr18Client; class NotificationService { private OneSignal $oneSignal; private LoggerInterface $logger; private EntityManagerInterface $em; private SolutionService $solutionService; private array $oneSignalConfig; public function __construct( array $oneSignalConfig, SolutionService $solutionService, LoggerInterface $logger, EntityManagerInterface $em ) { $this->oneSignalConfig = $oneSignalConfig; $this->logger = $logger; $this->em = $em; $this->solutionService = $solutionService; } private function initOneSignal(): void { $osConfig = $this->oneSignalConfig; $currentConfig = $osConfig[$this->solutionService->getCurrent()->getSlug()]; $config = new Config( $currentConfig['application_id'], $currentConfig['application_auth_key'], $currentConfig['auth_key'] ); $requestFactory = $streamFactory = new Psr17Factory(); $httpClient = new Psr18Client(); $this->oneSignal = new OneSignal($config, $httpClient, $requestFactory, $streamFactory); } public function sendReminder(Adresse $adresse): void { $this->logger->notice('Send reminder to address '.$adresse->getId()); $this->initOneSignal(); $this->oneSignal->notifications()->add([ 'contents' => [ 'en' => 'Asking control for '.$adresse, 'fr' => 'Contrôle demandé au '.$adresse, ], 'include_external_user_ids' => $this->getExternalUserIds($adresse->getUserAddresses()), 'channel_for_external_user_ids' => 'push', ]); $this->logger->debug( 'Send notifications for '.print_r($this->getExternalUserIds($adresse->getUserAddresses()), true) ); $this->storeReminder($adresse); } /** * @param UserAddress[]|Collection $userAddresses */ private function getExternalUserIds($userAddresses): array { $ids = []; $curDate = new \DateTime('today midnight'); foreach ($userAddresses as $userAddress) { if ( (null === $userAddress->getStartDate() || $userAddress->getStartDate() <= $curDate) && (null === $userAddress->getEndDate() || $userAddress->getEndDate() >= $curDate) ) { $ids[] = strval($userAddress->getUser()->getId()); } } return $ids; } private function storeReminder(Adresse $adresse): void { $reminder = new AddressReminder(); $reminder->setAddress($adresse); $this->em->persist($reminder); $this->em->flush(); } }
Save File
Cancel