<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\SitesBundle\EventSubscriber;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProjectViewLinksSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var Security
*/
protected Security $security;
/**
* @var RouterInterface
*/
protected RouterInterface $router;
/**
* @var EntityManagerInterface
*/
protected EntityManagerInterface $em;
/**
* @param TranslatorInterface $tr
* @param Security $security
* @param RouterInterface $router
* @param EntityManagerInterface $em
*/
public function __construct(
TranslatorInterface $tr,
Security $security,
RouterInterface $router,
EntityManagerInterface $em
) {
$this->tr = $tr;
$this->security = $security;
$this->router = $router;
$this->em = $em;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
if (CheckBundleInstall::exist('projects-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\ProjectsBundle\Event\ProjectViewLinksEvent');
return [
$reflectionClass->getConstant('LINKS') => 'links'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function links(object $event): void
{
if (!$this->security->isGranted('ROLE__SITES__READ_ONLY')) {
return;
}
$project = $this->em->getRepository('ProjectsBundle:Project')->find($event->getId());
$site = $project->getSite();
if (!$site) {
return;
}
$href = $this->router->generate('sites_bundle__site_view', ['id' => $site->getId()]);
$link = '<a href="' . $href . '" class="btn btn-sm btn-primary"><i class="fas fa-link"></i> ';
$link .= $this->tr->trans('Site:', [], 'SitesBundle') . ' ' . $site->getName() . '</a>';
$newLinks = [$link];
$event->setLinks(array_merge($event->getLinks(), $newLinks));
}
}