<?php
namespace App\EventListener;
use App\Services\ElasticModelFactory;
use App\Services\ElasticsearchService;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\BlogPost;
class BlogPostEventListener
{
/**
* @param DataObjectEvent $event
* @return void
*/
public function elasticsearchReindex(DataObjectEvent $event)
{
$object = $event->getObject();
if (!$object instanceof BlogPost) return;
$elasticService = new ElasticsearchService();
$elasticFactory = new ElasticModelFactory();
$elasticModel = $elasticFactory->make($object);
if (!$object->getPublished()) {
$elasticService->delete($elasticModel);
} else {
$elasticService->update($elasticModel);
}
}
/**
* @param DataObjectEvent $event
* @return void
*/
public function elasticsearchDelete(DataObjectEvent $event)
{
$object = $event->getObject();
if (!$object instanceof BlogPost) return;
$elasticService = new ElasticsearchService();
$elasticFactory = new ElasticModelFactory();
$elasticModel = $elasticFactory->make($object);
$elasticService->delete($elasticModel);
}
}