<?php
namespace App\EventListener;
use App\Entity\Answer;
use App\Entity\Blog;
use App\Entity\Category;
use App\Entity\Employee;
use App\Entity\Event;
use App\Entity\EventCategory;
use App\Entity\Location;
use App\Entity\Page;
use App\Entity\Product;
use App\Entity\Question;
use App\Entity\Sample;
use App\Entity\Tag;
use App\Entity\Testimonial;
use App\Entity\Translation\BlogTranslation;
use App\Entity\Translation\CategoryTranslation;
use App\Entity\Translation\EmployeeTranslation;
use App\Entity\Translation\EventCategoryTranslation;
use App\Entity\Translation\EventLocationTranslation;
use App\Entity\Translation\EventTranslation;
use App\Entity\Translation\PageTranslation;
use App\Entity\Translation\ProductTranslation;
use App\Entity\Translation\SampleTranslation;
use App\Entity\Translation\ShoppingAssistantAnswerTranslation;
use App\Entity\Translation\ShoppingAssistantQuestionTranslation;
use App\Entity\Translation\TagTranslation;
use App\Entity\Translation\TestimonialTranslation;
use App\Entity\Translation\USPTranslation;
use App\Entity\USP;
use App\Event\SiteUpdateEvent;
use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManagerInterface;
class TranslationUpdateEventListener
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var AnnotationReader
*/
protected $annotationReader;
protected $translationEntities = [
EmployeeTranslation::class => Employee::class,
ShoppingAssistantQuestionTranslation::class => Question::class,
TestimonialTranslation::class => Testimonial::class,
TagTranslation::class => Tag::class,
PageTranslation::class => Page::class,
ProductTranslation::class => Product::class,
EventLocationTranslation::class => Location::class,
ShoppingAssistantAnswerTranslation::class => Answer::class,
CategoryTranslation::class => Category::class,
USPTranslation::class => USP::class,
EventTranslation::class => Event::class,
EventCategoryTranslation::class => EventCategory::class,
SampleTranslation::class => Sample::class,
BlogTranslation::class => Blog::class,
];
/**
* TranslationUpdateEventListener constructor.
*
* @throws AnnotationException
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->annotationReader = new AnnotationReader();
}
/**
* @throws DBALException
*/
public function update(SiteUpdateEvent $event)
{
$event->getOutput()->writeln(\PHP_EOL);
foreach ($this->translationEntities as $translationEntity => $class) {
$translationTableName = $this->entityManager->getClassMetadata($translationEntity)->getTableName();
$updated = $this->entityManager->getConnection()
->exec(sprintf("UPDATE `%s` SET `object_class` = '%s'", $translationTableName, addslashes($class)));
if ($updated > 0) {
$event->getOutput()->writeln(
sprintf(
'<comment>Updating Translation:</comment> set object_class %s for table %s',
$class,
$translationTableName
)
);
}
}
}
}