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

Add task handler for PageTree update #31

Merged
merged 8 commits into from
Apr 7, 2020
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
33 changes: 0 additions & 33 deletions Controller/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ public function __construct(

/**
* Returns fields for tasks.
*
* @return Response
*/
public function cgetFieldsAction(): Response
{
Expand All @@ -131,10 +129,6 @@ public function cgetFieldsAction(): Response

/**
* Returns list of tasks.
*
* @param Request $request
*
* @return Response
*/
public function cgetAction(Request $request): Response
{
Expand Down Expand Up @@ -195,10 +189,6 @@ private function extendResponseItem(array $item): array
* Prepares list-builder.
*
* @param FieldDescriptorInterface[] $fieldDescriptors
* @param Request $request
* @param ListBuilderInterface $listBuilder
*
* @return ListBuilderInterface
*/
private function prepareListBuilder(array $fieldDescriptors, Request $request, ListBuilderInterface $listBuilder): ListBuilderInterface
{
Expand Down Expand Up @@ -238,8 +228,6 @@ private function prepareListBuilder(array $fieldDescriptors, Request $request, L
* Executes given list-builder and returns result.
*
* @param FieldDescriptorInterface[] $fieldDescriptors
* @param Request $request
* @param ListBuilderInterface $listBuilder
*
* @return mixed[]
*/
Expand All @@ -265,10 +253,6 @@ private function executeListBuilder(array $fieldDescriptors, Request $request, L
/**
* Returns task for given id.
*
* @param string $id
*
* @return Response
*
* @throws TaskNotFoundException
*/
public function getAction(string $id): Response
Expand All @@ -278,10 +262,6 @@ public function getAction(string $id): Response

/**
* Create new task.
*
* @param Request $request
*
* @return Response
*/
public function postAction(Request $request): Response
{
Expand Down Expand Up @@ -320,11 +300,6 @@ public function postAction(Request $request): Response

/**
* Update task with given id.
*
* @param string $id
* @param Request $request
*
* @return Response
*/
public function putAction(string $id, Request $request): Response
{
Expand Down Expand Up @@ -360,10 +335,6 @@ public function putAction(string $id, Request $request): Response

/**
* Removes task with given id.
*
* @param string $id
*
* @return Response
*/
public function deleteAction(string $id): Response
{
Expand All @@ -377,10 +348,6 @@ public function deleteAction(string $id): Response

/**
* Removes multiple tasks identified by ids parameter.
*
* @param Request $request
*
* @return Response
*/
public function cdeleteAction(Request $request): Response
{
Expand Down
5 changes: 0 additions & 5 deletions Controller/TaskHandlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public function __construct(TaskHandlerFactoryInterface $taskHandlerFactory)
$this->taskHandlerFactory = $taskHandlerFactory;
}

/**
* @param Request $request
*
* @return Response
*/
public function getAction(Request $request): Response
{
$entityClass = $this->getRequestParameter($request, 'entityClass', true);
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SuluAutomationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$loader->load('page_tree_route.xml');

$this->configurePersistence($config['objects'], $container);
}
Expand Down
8 changes: 8 additions & 0 deletions Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function getId(): string

/**
* {@inheritdoc}
*
* @return self
*/
public function setId(string $id): TaskInterface
{
Expand Down Expand Up @@ -109,6 +111,9 @@ public function getSchedule(): \DateTime
return $this->schedule;
}

/**
* @return self
*/
public function setSchedule(\DateTime $schedule): TaskInterface
{
$this->schedule = $schedule;
Expand Down Expand Up @@ -163,6 +168,9 @@ public function getTaskId(): string
return $this->taskId;
}

/**
* @return self
*/
public function setTaskId(string $taskId): TaskInterface
{
$this->taskId = $taskId;
Expand Down
8 changes: 0 additions & 8 deletions EventSubscriber/PHPTaskEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class PHPTaskEventSubscriber implements EventSubscriberInterface
*/
private $taskRepository;

/**
* @param RequestStack $requestStack
* @param TaskRepositoryInterface $taskRepository
*/
public function __construct(RequestStack $requestStack, TaskRepositoryInterface $taskRepository)
{
$this->requestStack = $requestStack;
Expand All @@ -56,8 +52,6 @@ public static function getSubscribedEvents(): array

/**
* Create and push new request to requests-stack.
*
* @param TaskEvent $event
*/
public function pushRequest(TaskEvent $event): void
{
Expand All @@ -82,8 +76,6 @@ public function pushRequest(TaskEvent $event): void

/**
* Pop request from request stack.
*
* @param TaskEvent $event
*/
public function popRequest(TaskEvent $event): void
{
Expand Down
2 changes: 0 additions & 2 deletions Events/CancelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ trait CancelTrait

/**
* Returns canceled.
*
* @return bool
*/
public function isCanceled(): bool
{
Expand Down
3 changes: 0 additions & 3 deletions Handler/BaseDocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public function handle($workload)

/**
* Handle given document.
*
* @param WorkflowStageBehavior $document
* @param string $locale
*/
abstract protected function handleDocument(WorkflowStageBehavior $document, string $locale): void;

Expand Down
73 changes: 73 additions & 0 deletions PageTree/AutomationPageTreeUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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

namespace Sulu\Bundle\AutomationBundle\PageTree;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\AutomationBundle\Entity\Task;
use Sulu\Bundle\AutomationBundle\Tasks\Manager\TaskManagerInterface;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Bundle\RouteBundle\PageTree\PageTreeUpdaterInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Schedules the route-update task.
*/
class AutomationPageTreeUpdater implements PageTreeUpdaterInterface
{
/**
* @var TaskManagerInterface
*/
private $taskManager;

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

/**
* @var RequestStack
*/
private $requestStack;

public function __construct(
TaskManagerInterface $taskManager,
EntityManagerInterface $entityManager,
RequestStack $requestStack
) {
$this->taskManager = $taskManager;
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
public function update(BasePageDocument $document)
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return;
}

$task = new Task();
$task->setEntityClass(BasePageDocument::class)
->setEntityId($document->getUuid())
->setLocale($document->getLocale())
->setHandlerClass(PageTreeRouteUpdateHandler::class)
->setSchedule(new \DateTime())
->setHost($request->getHost())
->setScheme($request->getScheme());

$this->taskManager->create($task);
$this->entityManager->flush();
}
}
117 changes: 117 additions & 0 deletions PageTree/PageTreeRouteUpdateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

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

namespace Sulu\Bundle\AutomationBundle\PageTree;

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sulu\Bundle\AutomationBundle\TaskHandler\AutomationTaskHandlerInterface;
use Sulu\Bundle\AutomationBundle\TaskHandler\TaskHandlerConfiguration;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Bundle\RouteBundle\PageTree\PageTreeUpdaterInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Task\Executor\RetryTaskHandlerInterface;
use Task\Lock\LockingTaskHandlerInterface;

/**
* Task-Handler to update page-tree-routes.
*/
class PageTreeRouteUpdateHandler implements AutomationTaskHandlerInterface, LockingTaskHandlerInterface, RetryTaskHandlerInterface
{
/**
* @var PageTreeUpdaterInterface
*/
private $routeUpdater;

/**
* @var DocumentManagerInterface
*/
private $documentManager;

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

public function __construct(
PageTreeUpdaterInterface $routeUpdater,
DocumentManagerInterface $documentManager,
EntityManagerInterface $entityManager
) {
$this->routeUpdater = $routeUpdater;
$this->documentManager = $documentManager;
$this->entityManager = $entityManager;
}

/**
* {@inheritdoc}
*/
public function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
return $optionsResolver->setRequired(['id', 'locale'])
->setAllowedTypes('id', 'string')
->setAllowedTypes('locale', 'string');
}

/**
* {@inheritdoc}
*/
public function supports($entityClass): bool
{
return is_subclass_of($entityClass, BasePageDocument::class);
}

/**
* {@inheritdoc}
*/
public function getConfiguration(): TaskHandlerConfiguration
{
return TaskHandlerConfiguration::create('sulu_automation.update_page_tree_route');
}

/**
* @param array<string, mixed> $workload
*/
public function handle($workload)
{
$this->entityManager->beginTransaction();

try {
/** @var BasePageDocument $document */
$document = $this->documentManager->find($workload['id'], $workload['locale']);
$this->routeUpdater->update($document);

$this->documentManager->flush();
$this->entityManager->commit();
} catch (Exception $exception) {
$this->entityManager->rollback();

throw $exception;
}
}

/**
* {@inheritdoc}
*/
public function getLockKey($workload)
{
return self::class;
}

/**
* {@inheritdoc}
*/
public function getMaximumAttempts()
{
return 3;
}
}
Loading