Skip to content

Commit

Permalink
Merge 8707c2b into 8fdc660
Browse files Browse the repository at this point in the history
  • Loading branch information
JZuidema committed Jan 28, 2020
2 parents 8fdc660 + 8707c2b commit 8e74d87
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/Kunstmaan/NodeBundle/Entity/NodeTranslation.php
Expand Up @@ -3,7 +3,7 @@
namespace Kunstmaan\NodeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
use Kunstmaan\NodeBundle\Form\NodeTranslationAdminType;
Expand Down Expand Up @@ -373,21 +373,33 @@ public function getDefaultAdminType()
}

/**
* @param EntityManager $em The entity manager
* @param string $type The type
* @param EntityManagerInterface $em The entity manager
* @param string $type The type
*
* @return object|null
*/
public function getRef(EntityManager $em, $type = 'public')
public function getRef(EntityManagerInterface $em, $type = 'public')
{
$nodeVersion = $this->getNodeVersion($type);
if ($nodeVersion) {
return $em->getRepository($nodeVersion->getRefEntityName())->find($nodeVersion->getRefId());

if ($nodeVersion instanceof NodeVersion) {
return $this->getRefByNodeVersion($em, $nodeVersion);
}

return null;
}

/**
* @param EntityManagerInterface $em The entity manager
* @param NodeVersion $nodeVersion
*
* @return object|null
*/
public function getRefByNodeVersion(EntityManagerInterface $em, NodeVersion $nodeVersion)
{
return $em->getRepository($nodeVersion->getRefEntityName())->find($nodeVersion->getRefId());
}

/**
* @param string $url
*
Expand Down
43 changes: 40 additions & 3 deletions src/Kunstmaan/NodeBundle/EventListener/SlugListener.php
Expand Up @@ -5,9 +5,13 @@
use Doctrine\ORM\EntityManager;
use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Entity\NodeVersion;
use Kunstmaan\NodeBundle\Event\Events;
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
use Kunstmaan\NodeBundle\Router\SlugRouter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
Expand Down Expand Up @@ -36,8 +40,11 @@ class SlugListener
* @param ControllerResolverInterface $resolver
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EntityManager $em, ControllerResolverInterface $resolver, EventDispatcherInterface $eventDispatcher)
{
public function __construct(
EntityManager $em,
ControllerResolverInterface $resolver,
EventDispatcherInterface $eventDispatcher
) {
$this->em = $em;
$this->resolver = $resolver;
$this->eventDispatcher = $eventDispatcher;
Expand Down Expand Up @@ -65,7 +72,8 @@ public function onKernelController($event)
if (!($nodeTranslation instanceof NodeTranslation)) {
throw new \Exception('Invalid _nodeTranslation value found in request attributes');
}
$entity = $nodeTranslation->getRef($this->em);

$entity = $this->getEntity($nodeTranslation, $request);

// If the entity is an instance of the SlugActionInterface, change the controller
if ($entity instanceof SlugActionInterface) {
Expand All @@ -86,4 +94,33 @@ public function onKernelController($event)
$event->setController($this->resolver->getController($request));
}
}

/**
* @param NodeTranslation $nodeTranslation
* @param Request $request
*
* @return null|object
*/
private function getEntity(NodeTranslation $nodeTranslation, Request $request)
{
$versionId = $request->query->get('version');

if ($request->attributes->get('_route') !== SlugRouter::$SLUG_PREVIEW || $versionId === null) {
return $nodeTranslation->getRef($this->em);
}

/** @var NodeVersionRepository $nodeVersionRepository */
$nodeVersionRepository = $this->em->getRepository(NodeVersion::class);

$nodeVersion = $nodeVersionRepository->findOneBy([
'nodeTranslation' => $nodeTranslation,
'id' => $versionId,
]);

if (!$nodeVersion instanceof NodeVersion) {
return null;
}

return $nodeTranslation->getRefByNodeVersion($this->em, $nodeVersion);
}
}

0 comments on commit 8e74d87

Please sign in to comment.