src/Subscriber/Content/ContentCheckPriceSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Entity\ContentImageset;
  4. use App\Entity\ContentVideo;
  5. use App\Event\Content\ContentCheckedEvent;
  6. use App\Lib\Content\ContentServiceAwareInterface;
  7. use App\Service\Content\ContentService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ContentCheckPriceSubscriber implements EventSubscriberInterfaceContentServiceAwareInterface
  10. {
  11.     protected ContentService $service;
  12.     /**
  13.      * ContentCheckPriceSubscriber constructor.
  14.      */
  15.     public function __construct(ContentService $service)
  16.     {
  17.         $this->setContentService($service);
  18.     }
  19.     public function setContentService(ContentService $service): ContentServiceAwareInterface
  20.     {
  21.         $this->service $service;
  22.         return $this;
  23.     }
  24.     public function getContentService(): ContentService
  25.     {
  26.         return $this->service;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ContentCheckedEvent::class => [
  32.                 ['onCheckedUpdatePrice'0],
  33.             ],
  34.         ];
  35.     }
  36.     /**
  37.      * @throws \Doctrine\ORM\ORMException
  38.      * @throws \Doctrine\ORM\OptimisticLockException
  39.      */
  40.     public function onCheckedUpdatePrice(ContentCheckedEvent $event)
  41.     {
  42.         $content $event->getContent();
  43.         $abstractContent $content->getContent();
  44.         if ($content->getPrice() > || null === $abstractContent) {
  45.             return;
  46.         }
  47.         if ($abstractContent instanceof ContentImageset) {
  48.             $content->setPrice($abstractContent->getPics() * $abstractContent->getCostPerUnit());
  49.         }
  50.         if ($abstractContent instanceof ContentVideo) {
  51.             $content->setPrice($abstractContent->getDuration() * $abstractContent->getCostPerUnit());
  52.         }
  53.         $content->setUpdatedAt(new \DateTime());
  54.         $this->service->storeEntity($content);
  55.     }
  56. }