<?php
namespace App\Subscriber\Content;
use App\Entity\ContentImageset;
use App\Entity\ContentVideo;
use App\Event\Content\ContentCheckedEvent;
use App\Lib\Content\ContentServiceAwareInterface;
use App\Service\Content\ContentService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentCheckPriceSubscriber implements EventSubscriberInterface, ContentServiceAwareInterface
{
protected ContentService $service;
/**
* ContentCheckPriceSubscriber constructor.
*/
public function __construct(ContentService $service)
{
$this->setContentService($service);
}
public function setContentService(ContentService $service): ContentServiceAwareInterface
{
$this->service = $service;
return $this;
}
public function getContentService(): ContentService
{
return $this->service;
}
public static function getSubscribedEvents(): array
{
return [
ContentCheckedEvent::class => [
['onCheckedUpdatePrice', 0],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onCheckedUpdatePrice(ContentCheckedEvent $event)
{
$content = $event->getContent();
$abstractContent = $content->getContent();
if ($content->getPrice() > 0 || null === $abstractContent) {
return;
}
if ($abstractContent instanceof ContentImageset) {
$content->setPrice($abstractContent->getPics() * $abstractContent->getCostPerUnit());
}
if ($abstractContent instanceof ContentVideo) {
$content->setPrice($abstractContent->getDuration() * $abstractContent->getCostPerUnit());
}
$content->setUpdatedAt(new \DateTime());
$this->service->storeEntity($content);
}
}