vendor/shopware/administration/Notification/NotificationService.php line 60

Open in your IDE?
  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\EntityRepositoryInterface;
  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.     private EntityRepositoryInterface $notificationRepository;
  20.     public function __construct(EntityRepositoryInterface $notificationRepository)
  21.     {
  22.         $this->notificationRepository $notificationRepository;
  23.     }
  24.     public function createNotification(array $dataContext $context): void
  25.     {
  26.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($data): void {
  27.             $this->notificationRepository->create([$data], $context);
  28.         });
  29.     }
  30.     public function getNotifications(Context $contextint $limit, ?string $latestTimestamp): array
  31.     {
  32.         $source $context->getSource();
  33.         if (!$source instanceof AdminApiSource) {
  34.             throw new InvalidContextSourceException(AdminApiSource::class, \get_class($context->getSource()));
  35.         }
  36.         $criteria = new Criteria();
  37.         $isAdmin $source->isAdmin();
  38.         if (!$isAdmin) {
  39.             $criteria->addFilter(new EqualsFilter('adminOnly'false));
  40.         }
  41.         if ($latestTimestamp) {
  42.             $criteria->addFilter(new RangeFilter('createdAt', [
  43.                 RangeFilter::GT => $latestTimestamp,
  44.             ]));
  45.         }
  46.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  47.         $criteria->setLimit($limit);
  48.         $notifications $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($criteria) {
  49.             /** @var NotificationCollection $notifications */
  50.             $notifications $this->notificationRepository->search($criteria$context)->getEntities();
  51.             return $notifications;
  52.         });
  53.         if ($notifications->count() === 0) {
  54.             return [
  55.                 'notifications' => new NotificationCollection(),
  56.                 'timestamp' => null,
  57.             ];
  58.         }
  59.         /** @var NotificationEntity $notification */
  60.         $notification $notifications->last();
  61.         /** @var \DateTimeInterface $latestTimestamp */
  62.         $latestTimestamp $notification->getCreatedAt();
  63.         $latestTimestamp $latestTimestamp->format(Defaults::STORAGE_DATE_TIME_FORMAT);
  64.         if ($isAdmin) {
  65.             return [
  66.                 'notifications' => $notifications,
  67.                 'timestamp' => $latestTimestamp,
  68.             ];
  69.         }
  70.         $notifications $this->formatNotifications($notifications$source);
  71.         return [
  72.             'notifications' => $notifications,
  73.             'timestamp' => $latestTimestamp,
  74.         ];
  75.     }
  76.     private function formatNotifications(NotificationCollection $notificationsAdminApiSource $source): NotificationCollection
  77.     {
  78.         $responseNotifications = new NotificationCollection();
  79.         /** @var NotificationEntity $notification */
  80.         foreach ($notifications as $notification) {
  81.             if ($this->isAllow($notification->getRequiredPrivileges(), $source)) {
  82.                 $responseNotifications->add($notification);
  83.             }
  84.         }
  85.         return $responseNotifications;
  86.     }
  87.     private function isAllow(array $privilegesAdminApiSource $source): bool
  88.     {
  89.         foreach ($privileges as $privilege) {
  90.             if (!$source->isAllowed($privilege)) {
  91.                 return false;
  92.             }
  93.         }
  94.         return true;
  95.     }
  96. }