src/Cms/LogBundle/Service/FeedingService.php line 128

Open in your IDE?
  1. <?php
  2. namespace Cms\LogBundle\Service;
  3. use Cms\CoreBundle\Service\ContextManager;
  4. use Cms\CoreBundle\Util\DateTimeUtils;
  5. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  6. use Cms\LogBundle\Entity\RecentFeed;
  7. use Cms\ModuleBundle\Entity\Draft;
  8. use Cms\ModuleBundle\Entity\ModuleEntity;
  9. use Cms\ModuleBundle\Entity\Proxy;
  10. use DateTime;
  11. use Platform\SecurityBundle\Entity\Identity\Account;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. final class FeedingService implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var array|RecentFeed[]
  18.      */
  19.     protected $entries = [];
  20.     /**
  21.      * @var EntityManager
  22.      */
  23.     protected $em;
  24.     /**
  25.      * @var ContextManager
  26.      */
  27.     protected $cm;
  28.     /**
  29.      * @param EntityManager $em
  30.      * @param ContextManager $cm
  31.      */
  32.     public function __construct(EntityManager $emContextManager $cm)
  33.     {
  34.         $this->em $em;
  35.         $this->cm $cm;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             KernelEvents::TERMINATE => ['onKernelTerminate'],
  44.         ];
  45.     }
  46.     /**
  47.      * @param ModuleEntity $entity
  48.      * @param DateTime|null $timestamp
  49.      * @param Account|null $account
  50.      * @return $this
  51.      */
  52.     public function log(ModuleEntity $entity, ?DateTime $timestamp null, ?Account $account null): self
  53.     {
  54.         // get the proxy for the entity that we have
  55.         $proxy null;
  56.         switch (true) {
  57.             case $entity instanceof Proxy:
  58.                 $proxy $entity;
  59.                 break;
  60.             case $entity instanceof Draft:
  61.                 $proxy $entity->getProxy();
  62.                 break;
  63.         }
  64.         if ( ! $proxy instanceof Proxy) {
  65.             throw new \Exception();
  66.         }
  67.         // get the currently logged in, effective, user if no account was given specifically
  68.         if (empty($account)) {
  69.             $account $this->cm->getGlobalContext()->getEffectiveAccount();
  70.         }
  71.         // if the user is not set, we want to skip this
  72.         if ( ! empty($account)) {
  73.             // try to find the existing log, create a new one if not
  74.             $entry $this->em->getRepository(RecentFeed::class)->findOneByAccountAndProxy($account$proxy);
  75.             if (empty($entry)) {
  76.                 $entry = (new RecentFeed())
  77.                     ->setAccount($account)
  78.                     ->setProxy($proxy);
  79.             }
  80.             // save the name
  81.             $entry->setName($proxy->getData()->ui());
  82.             // need to force things when given
  83.             if ( ! $entry->isCreated()) {
  84.                 $entry
  85.                     ->autoCreatedBy(false)
  86.                     ->setCreatedBy($accountfalse);
  87.                 if ( ! empty($timestamp)) {
  88.                     $entry
  89.                         ->autoCreatedAt(false)
  90.                         ->setCreatedAt($timestampfalse);
  91.                 }
  92.             } else {
  93.                 $entry
  94.                     ->autoUpdatedBy(false)
  95.                     ->setUpdatedBy($accountfalse);
  96.                 if ( ! empty($timestamp)) {
  97.                     $entry
  98.                         ->autoUpdatedAt(false)
  99.                         ->setUpdatedAt($timestampfalse);
  100.                 }
  101.             }
  102.             // need to touch the entity in some way to force it to update in case all other data is the same
  103.             $entry->setChecksum(DateTimeUtils::now()->getTimestamp());
  104.             // spool the entry
  105.             $this->entries[] = $entry;
  106.         }
  107.         return $this;
  108.     }
  109.     /**
  110.      *
  111.      */
  112.     public function onKernelTerminate(): void
  113.     {
  114.         if ( ! empty($this->entries)) {
  115.             $this->em->saveAll($this->entries);
  116.             $this->entries = [];
  117.         }
  118.     }
  119. }