<?php
namespace Products\NotificationsBundle\Service;
use Products\NotificationsBundle\Entity\AbstractNotification;
use Products\NotificationsBundle\Entity\Notifications\NotificationInterface;
use Products\NotificationsBundle\Entity\Profile;
use Products\NotificationsBundle\Entity\Student;
use Products\NotificationsBundle\Service\Notifications\NotificationsConfigService;
use Products\NotificationsBundle\Util\ListBuilder\AbstractListBuilder;
use Products\NotificationsBundle\Util\MessageContentGenerator;
class MergeParamsProvider
{
/**
* @var NotificationsConfigService
*/
private NotificationsConfigService $notificationsConfigService;
/**
* @param NotificationsConfigService $notificationsConfigService
*/
public function __construct(NotificationsConfigService $notificationsConfigService)
{
$this->notificationsConfigService = $notificationsConfigService;
}
/**
* @param AbstractNotification $message
* @return array
*/
public function get(NotificationInterface $notification): array
{
$config = $this->notificationsConfigService->getNotificationsConfig();
// prepare Profile and Student metadata from notification config
$profileMetadata = [];
$studentMetadata = [];
foreach ($config->getConfig()->getSchemas() as $schema) {
if ($schema->getFilterEntityProperty() !== 'metadata') {
continue;
}
switch ($schema->getFilterEntity()) {
case AbstractListBuilder::ENTITIES__PROFILES:
$profileMetadata[$schema->getFilterJsonKey()] = $schema->getFilterJsonKey();
break;
case AbstractListBuilder::ENTITIES__STUDENTS:
$studentMetadata[$schema->getFilterJsonKey()] = $schema->getFilterJsonKey();
break;
}
}
// create params with dummy Profile and Student objects
$params = (new MessageContentGenerator())
->setNotification($notification instanceof AbstractNotification ? $notification : null)
->setProfile((new Profile())->setMetadata($profileMetadata))
->setStudent((new Student())->setMetadata($studentMetadata))
->setLocale($notification->getLocale())
->setTiming()
->getParams();
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($params));
$result = [];
foreach ($iterator as $value) {
$keys = [];
foreach (range(0, $iterator->getDepth()) as $depth) {
$keys[] = $iterator->getSubIterator($depth)->key();
}
$result[implode('.', $keys)] = implode('.', $keys);
}
// remove debug field
unset($result['debug']);
return array_keys($result);
}
}