<?php
namespace App\Package\MediaThumbable\Tools\ThumbGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Package\MediaThumbable\Tools\ThumbGenerator\ThumbGenerator,
App\Package\Admin\Main\Event\EntityUpdateEvent,
App\Package\Core\EntityInterface\SimpleEntityInterface;
/**
* ThumbGeneratorSubscriber
*
* Subscriber which listens for entity update event (created or updated)
* - if entity has thumbs then processes image path field and either generates,
* removes thumbs or calls "generateThumbs" function from entity (passes
* ThumbGenerator object as argument of "generateThumbs")
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright Openform
* @since 03.2019
*/
class ThumbGeneratorSubscriber implements EventSubscriberInterface
{
/**
* @var ThumbGenerator
*/
protected $thumbGenerator;
/**
* @param ThumbGenerator $thumbGenerator
*/
public function __construct(ThumbGenerator $thumbGenerator)
{
$this->thumbGenerator = $thumbGenerator;
}
/**
* @return array
*/
public static function getSubscribedEvents() : array
{
return [
EntityUpdateEvent::NAME => [
[ 'onEntityUpdate', 800 ],
]
];
}
/**
* On entity update event
*
* @param EntityUpdateEvent $event
*
* @return void
*/
public function onEntityUpdate(EntityUpdateEvent $event) : void
{
$entity = $event->getUpdatedEntity();
$this->processGenerateThumbs( $entity );
return;
}
/**
* Processes generate thumbs for entity
*
* @param SimpleEntityInterface $entity
*
* @return void
*/
protected function processGenerateThumbs(SimpleEntityInterface $entity) : void
{
if (method_exists($entity, 'generateThumbs')) {
$entity->generateThumbs( $this->thumbGenerator );
return;
}
if (! method_exists($entity, 'getThumb')) {
return;
}
if (
$entity->getImagePath() &&
$entity->getOldImagePath() !== $entity->getImagePath()
) {
$this->thumbGenerator->generate(
$entity,
$entity->getThumb(),
'getImagePath'
);
return;
}
if (! $entity->getImagePath()) {
$this->thumbGenerator->removeThumbs($entity);
}
return;
}
}