src/Products/NotificationsBundle/Service/MergeParamsProvider.php line 34

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Service;
  3. use Products\NotificationsBundle\Entity\AbstractNotification;
  4. use Products\NotificationsBundle\Entity\Notifications\NotificationInterface;
  5. use Products\NotificationsBundle\Entity\Profile;
  6. use Products\NotificationsBundle\Entity\Student;
  7. use Products\NotificationsBundle\Service\Notifications\NotificationsConfigService;
  8. use Products\NotificationsBundle\Util\ListBuilder\AbstractListBuilder;
  9. use Products\NotificationsBundle\Util\MessageContentGenerator;
  10. class MergeParamsProvider
  11. {
  12.     /**
  13.      * @var NotificationsConfigService
  14.      */
  15.     private NotificationsConfigService $notificationsConfigService;
  16.     /**
  17.      * @param NotificationsConfigService $notificationsConfigService
  18.      */
  19.     public function __construct(NotificationsConfigService $notificationsConfigService)
  20.     {
  21.         $this->notificationsConfigService $notificationsConfigService;
  22.     }
  23.     /**
  24.      * @param AbstractNotification $message
  25.      * @return array
  26.      */
  27.     public function get(NotificationInterface $notification): array
  28.     {
  29.         $config $this->notificationsConfigService->getNotificationsConfig();
  30.         // prepare Profile and Student metadata from notification config
  31.         $profileMetadata = [];
  32.         $studentMetadata = [];
  33.         foreach ($config->getConfig()->getSchemas() as $schema) {
  34.             if ($schema->getFilterEntityProperty() !== 'metadata') {
  35.                 continue;
  36.             }
  37.             switch ($schema->getFilterEntity()) {
  38.                 case AbstractListBuilder::ENTITIES__PROFILES:
  39.                     $profileMetadata[$schema->getFilterJsonKey()] = $schema->getFilterJsonKey();
  40.                     break;
  41.                 case AbstractListBuilder::ENTITIES__STUDENTS:
  42.                     $studentMetadata[$schema->getFilterJsonKey()] = $schema->getFilterJsonKey();
  43.                     break;
  44.             }
  45.         }
  46.         // create params with dummy Profile and Student objects
  47.         $params = (new MessageContentGenerator())
  48.             ->setNotification($notification instanceof AbstractNotification $notification null)
  49.             ->setProfile((new Profile())->setMetadata($profileMetadata))
  50.             ->setStudent((new Student())->setMetadata($studentMetadata))
  51.             ->setLocale($notification->getLocale())
  52.             ->setTiming()
  53.             ->getParams();
  54.         $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($params));
  55.         $result = [];
  56.         foreach ($iterator as $value) {
  57.             $keys = [];
  58.             foreach (range(0$iterator->getDepth()) as $depth) {
  59.                 $keys[] = $iterator->getSubIterator($depth)->key();
  60.             }
  61.             $result[implode('.'$keys)] = implode('.'$keys);
  62.         }
  63.         // remove debug field
  64.         unset($result['debug']);
  65.         return array_keys($result);
  66.     }
  67. }