src/Administration/Notification/NotificationService.php line 55

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Administration\Notification;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  5. use Shopware\Core\Framework\Api\Context\Exception\InvalidContextSourceException;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\Framework\Log\Package;
  13. /**
  14.  * @internal
  15.  */
  16. #[Package('administration')]
  17. class NotificationService
  18. {
  19.     public function __construct(private readonly EntityRepository $notificationRepository)
  20.     {
  21.     }
  22.     public function createNotification(array $dataContext $context): void
  23.     {
  24.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($data): void {
  25.             $this->notificationRepository->create([$data], $context);
  26.         });
  27.     }
  28.     public function getNotifications(Context $contextint $limit, ?string $latestTimestamp): array
  29.     {
  30.         $source $context->getSource();
  31.         if (!$source instanceof AdminApiSource) {
  32.             throw new InvalidContextSourceException(AdminApiSource::class, $context->getSource()::class);
  33.         }
  34.         $criteria = new Criteria();
  35.         $isAdmin $source->isAdmin();
  36.         if (!$isAdmin) {
  37.             $criteria->addFilter(new EqualsFilter('adminOnly'false));
  38.         }
  39.         if ($latestTimestamp) {
  40.             $criteria->addFilter(new RangeFilter('createdAt', [
  41.                 RangeFilter::GT => $latestTimestamp,
  42.             ]));
  43.         }
  44.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  45.         $criteria->setLimit($limit);
  46.         $notifications $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($criteria) {
  47.             /** @var NotificationCollection $notifications */
  48.             $notifications $this->notificationRepository->search($criteria$context)->getEntities();
  49.             return $notifications;
  50.         });
  51.         if ($notifications->count() === 0) {
  52.             return [
  53.                 'notifications' => new NotificationCollection(),
  54.                 'timestamp' => null,
  55.             ];
  56.         }
  57.         /** @var NotificationEntity $notification */
  58.         $notification $notifications->last();
  59.         /** @var \DateTimeInterface $latestTimestamp */
  60.         $latestTimestamp $notification->getCreatedAt();
  61.         $latestTimestamp $latestTimestamp->format(Defaults::STORAGE_DATE_TIME_FORMAT);
  62.         if ($isAdmin) {
  63.             return [
  64.                 'notifications' => $notifications,
  65.                 'timestamp' => $latestTimestamp,
  66.             ];
  67.         }
  68.         $notifications $this->formatNotifications($notifications$source);
  69.         return [
  70.             'notifications' => $notifications,
  71.             'timestamp' => $latestTimestamp,
  72.         ];
  73.     }
  74.     private function formatNotifications(NotificationCollection $notificationsAdminApiSource $source): NotificationCollection
  75.     {
  76.         $responseNotifications = new NotificationCollection();
  77.         /** @var NotificationEntity $notification */
  78.         foreach ($notifications as $notification) {
  79.             if ($this->isAllow($notification->getRequiredPrivileges(), $source)) {
  80.                 $responseNotifications->add($notification);
  81.             }
  82.         }
  83.         return $responseNotifications;
  84.     }
  85.     private function isAllow(array $privilegesAdminApiSource $source): bool
  86.     {
  87.         foreach ($privileges as $privilege) {
  88.             if (!$source->isAllowed($privilege)) {
  89.                 return false;
  90.             }
  91.         }
  92.         return true;
  93.     }
  94. }