<?php
namespace App\EventListener;
use App\Services\ElasticModelFactory;
use App\Services\ElasticsearchService;
use Pimcore\Event\Model\DocumentEvent;
class GlossaryEventListener
{
/**
* @param DocumentEvent $event
* @return void
*/
public function elasticsearchReindex(DocumentEvent $event)
{
$document = $event->getDocument();
if (!method_exists($document, 'getController') || !method_exists($document, 'getTemplate')) return;
if (
$document->getController() != 'App\Controller\Glossary\GlossaryController::glossarySingleAction' &&
$document->getTemplate() != 'glossary/glossary_single.html.twig'
) return;
$elasticService = new ElasticsearchService();
$elasticFactory = new ElasticModelFactory();
$elasticModel = $elasticFactory->make($document);
if (!$document->getPublished()) {
$elasticService->delete($elasticModel);
} else {
$elasticService->update($elasticModel);
}
}
/**
* @param DocumentEvent $event
* @return void
*/
public function elasticsearchDelete(DocumentEvent $event)
{
$document = $event->getDocument();
if (!method_exists($document, 'getController') || !method_exists($document, 'getTemplate')) return;
if (
$document->getController() != 'App\Controller\Glossary\GlossaryController::glossarySingleAction' &&
$document->getTemplate() != 'glossary/glossary_single.html.twig'
) return;
$elasticService = new ElasticsearchService();
$elasticFactory = new ElasticModelFactory();
$elasticModel = $elasticFactory->make($document);
$elasticService->delete($elasticModel);
}
}