Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted routable-behavior from article-subscriber #111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Sulu\Bundle\ArticleBundle\Document;

use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\AuthorBehavior;
use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
Expand Down Expand Up @@ -44,7 +44,7 @@ class ArticleDocument implements
LocalizedStructureBehavior,
LocalizedAuditableBehavior,
DateShardingBehavior,
RoutableInterface,
RoutableBehavior,
ExtensionBehavior,
WorkflowStageBehavior,
VersionBehavior,
Expand Down Expand Up @@ -178,9 +178,7 @@ public function getUuid()
}

/**
* Set uuid.
*
* @param string $uuid
* {@inheritdoc}
*/
public function setUuid($uuid)
{
Expand Down Expand Up @@ -257,19 +255,15 @@ public function setRoute(RouteInterface $route)
}

/**
* Returns route-path.
*
* @return string
* {@inheritdoc}
*/
public function getRoutePath()
{
return $this->routePath;
}

/**
* Set route-path.
*
* @param string $routePath
* {@inheritdoc}
*/
public function setRoutePath($routePath)
{
Expand Down
43 changes: 43 additions & 0 deletions Document/Behavior/RoutableBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Document\Behavior;

use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Component\DocumentManager\Behavior\Mapping\LocaleBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior;

/**
* This behavior has to be attached to documents which should have a sulu-route.
*/
interface RoutableBehavior extends RoutableInterface, UuidBehavior, LocaleBehavior
{
/**
* Returns route-path.
*
* @return string
*/
public function getRoutePath();

/**
* Set route-path.
*
* @param string $routePath
*/
public function setRoutePath($routePath);

/**
* Set uuid.
*
* @param string $uuid
*/
public function setUuid($uuid);
}
129 changes: 3 additions & 126 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@

namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
use Sulu\Component\DocumentManager\Event\FlushEvent;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
use Sulu\Component\DocumentManager\Events;
Expand All @@ -41,21 +36,6 @@ class ArticleSubscriber implements EventSubscriberInterface
*/
private $liveIndexer;

/**
* @var RouteManagerInterface
*/
private $routeManager;

/**
* @var RouteRepositoryInterface
*/
private $routeRepository;

/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var DocumentManagerInterface
*/
Expand All @@ -74,24 +54,15 @@ class ArticleSubscriber implements EventSubscriberInterface
/**
* @param IndexerInterface $indexer
* @param IndexerInterface $liveIndexer
* @param RouteManagerInterface $routeManager
* @param RouteRepositoryInterface $routeRepository
* @param EntityManagerInterface $entityManager
* @param DocumentManagerInterface $documentManager
*/
public function __construct(
IndexerInterface $indexer,
IndexerInterface $liveIndexer,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
EntityManagerInterface $entityManager,
DocumentManagerInterface $documentManager
) {
$this->indexer = $indexer;
$this->liveIndexer = $liveIndexer;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->entityManager = $entityManager;
$this->documentManager = $documentManager;
}

Expand All @@ -101,78 +72,15 @@ public function __construct(
public static function getSubscribedEvents()
{
return [
Events::HYDRATE => [['handleHydrate', -500]],
Events::PERSIST => [['handleRouteUpdate', 1], ['handleRoute', 0], ['handleScheduleIndex', -500]],
Events::PERSIST => [['handleScheduleIndex', -500]],
Events::REMOVE => [['handleRemove', -500], ['handleRemoveLive', -500]],
Events::METADATA_LOAD => 'handleMetadataLoad',
Events::PUBLISH => [['handleScheduleIndexLive', 0], ['handleScheduleIndex', 0]],
Events::UNPUBLISH => 'handleUnpublish',
Events::CONFIGURE_OPTIONS => 'configureOptions',
Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
];
}

/**
* Load route for article-document.
*
* @param AbstractMappingEvent $event
*/
public function handleHydrate(AbstractMappingEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument || null === $document->getRoutePath()) {
return;
}

$route = $this->routeRepository->findByPath($document->getRoutePath(), $document->getOriginalLocale());
if (!$route) {
return;
}

$document->setRoute($route);
}

/**
* Generate route for article-document.
*
* @param AbstractMappingEvent $event
*/
public function handleRoute(AbstractMappingEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument || null !== $document->getRoutePath()) {
return;
}

$document->setUuid($event->getNode()->getIdentifier());

$route = $this->routeManager->create($document, $event->getOption('route_path'));
$this->entityManager->persist($route);
$this->entityManager->flush();
}

/**
* Update route for article-document if route-path was changed.
*
* @param AbstractMappingEvent $event
*/
public function handleRouteUpdate(AbstractMappingEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument
|| null === $document->getRoute()
|| null === ($routePath = $event->getOption('route_path'))
) {
return;
}

$route = $this->routeManager->update($document, $routePath);
$document->setRoutePath($route->getPath());
$this->entityManager->persist($route);
$this->entityManager->flush();
}

/**
* Schedule article document for index.
*
Expand Down Expand Up @@ -222,7 +130,8 @@ public function handleFlush(FlushEvent $event)

foreach ($this->documents as $document) {
$this->indexer->index(
$this->documentManager->find($document['uuid'],
$this->documentManager->find(
$document['uuid'],
$document['locale']
)
);
Expand Down Expand Up @@ -319,36 +228,4 @@ public function handleRemoveLive($event)
$this->liveIndexer->remove($document);
$this->liveIndexer->flush();
}

/**
* Add route to metadata.
*
* @param MetadataLoadEvent $event
*/
public function handleMetadataLoad(MetadataLoadEvent $event)
{
if ($event->getMetadata()->getClass() !== ArticleDocument::class) {
return;
}

$metadata = $event->getMetadata();
$metadata->addFieldMapping(
'routePath',
[
'encoding' => 'system_localized',
'property' => 'routePath',
]
);
}

/**
* Add route-path to options.
*
* @param ConfigureOptionsEvent $event
*/
public function configureOptions(ConfigureOptionsEvent $event)
{
$options = $event->getOptions();
$options->setDefaults(['route_path' => null]);
}
}
Loading