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

Added fake-request with host and scheme #12

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
21 changes: 19 additions & 2 deletions Controller/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,17 @@ public function getAction($id)
*/
public function postAction(Request $request)
{
$data = array_merge(
[
'scheme' => $request->getScheme(),
'host' => $request->getHost(),
],
array_filter($request->request->all())
);

$manager = $this->getTaskManager();
$task = $this->get('serializer')->deserialize(
json_encode(array_filter($request->request->all())),
json_encode($data),
Task::class,
'json',
DeserializationContext::create()->setGroups(['api'])
Expand All @@ -230,8 +238,17 @@ public function postAction(Request $request)
*/
public function putAction($id, Request $request)
{
$data = array_merge(
[
'id' => $id,
'scheme' => $request->getScheme(),
'host' => $request->getHost(),
],
array_filter($request->request->all())
);

$task = $this->get('serializer')->deserialize(
json_encode(array_merge(['id' => $id], $request->request->all())),
json_encode($data),
Task::class,
'json',
DeserializationContext::create()->setGroups(['api'])
Expand Down
8 changes: 8 additions & 0 deletions Entity/DoctrineTaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function findById($id)
return $this->find($id);
}

/**
* {@inheritdoc}
*/
public function findByTaskId($id)
{
return $this->findOneBy(['taskId' => $id]);
}

/**
* {@inheritdoc}
*/
Expand Down
54 changes: 54 additions & 0 deletions Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class Task implements TaskInterface
*/
private $taskId;

/**
* @var string
*/
private $host;

/**
* @var string
*/
private $scheme;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -210,6 +220,50 @@ public function setTaskId($taskId)
return $this;
}

/**
* {@inheritdoc}
*/
public function getHost()
{
return $this->host;
}

/**
* Set host.
*
* @param string $host
*
* @return $this
*/
public function setHost($host)
{
$this->host = $host;

return $this;
}

/**
* {@inheritdoc}
*/
public function getScheme()
{
return $this->scheme;
}

/**
* Set scheme.
*
* @param string $scheme
*
* @return $this
*/
public function setScheme($scheme)
{
$this->scheme = $scheme;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
90 changes: 90 additions & 0 deletions EventSubscriber/PHPTaskEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Sulu\Bundle\AutomationBundle\EventSubscriber;

use Sulu\Bundle\AutomationBundle\Tasks\Model\TaskRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Task\Event\Events;
use Task\Event\TaskEvent;

/**
* Fake-Request handling for tasks.
*/
class PHPTaskEventSubscriber implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;

/**
* @var TaskRepositoryInterface
*/
private $taskRepository;

/**
* @param RequestStack $requestStack
* @param TaskRepositoryInterface $taskRepository
*/
public function __construct(RequestStack $requestStack, TaskRepositoryInterface $taskRepository)
{
$this->requestStack = $requestStack;
$this->taskRepository = $taskRepository;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::TASK_BEFORE => ['pushRequest'],
Events::TASK_AFTER => ['popRequest'],
];
}

/**
* Create and push new request to requests-stack.
*
* @param TaskEvent $event
*/
public function pushRequest(TaskEvent $event)
{
$task = $this->taskRepository->findByTaskId($event->getTask()->getUuid());
if (!$task) {
// current task is not managed by this bundle

return;
}

$request = new Request(
[],
[],
['_task_id' => $event->getTask()->getUuid()],
[],
[],
['SERVER_NAME' => $task->getHost(), 'HTTPS' => $task->getScheme() === 'http' ? 'off' : 'on']
);

$this->requestStack->push($request);
}

/**
* Pop request from request stack.
*
* @param TaskEvent $event
*/
public function popRequest(TaskEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if (!$request || $request->attributes->get('_task_id') !== $event->getTask()->getUuid()) {
// current request was not created for current task

return;
}

$this->requestStack->pop();
}
}
7 changes: 3 additions & 4 deletions Handler/BaseDocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ abstract protected function handleDocument(WorkflowStageBehavior $document, $loc
*/
public function configureOptionsResolver(OptionsResolver $optionsResolver)
{
return $optionsResolver->setRequired(['id', 'locale'])->setAllowedTypes('id', 'string')->setAllowedTypes(
'locale',
'string'
);
return $optionsResolver->setRequired(['id', 'locale'])
->setAllowedTypes('id', 'string')
->setAllowedTypes('locale', 'string');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/doctrine/Task.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<field name="schedule" type="datetime" column="schedule"/>
<field name="locale" type="string" column="locale" length="5"/>

<field name="scheme" type="string" column="scheme" length="5"/>
<field name="host" type="string" column="host"/>

<field name="taskId" type="guid" nullable="true">
<options>
<option name="references">
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/serializer/Task.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<property name="created" type="DateTime" expose="true" groups="fullTask,partialTask"/>
<property name="changed" type="DateTime" expose="true" groups="fullTask,partialTask"/>

<property name="scheme" type="string" expose="true" groups="fullTask,partialTask,api"/>
<property name="host" type="string" expose="true" groups="fullTask,partialTask,api"/>

<virtual-property method="getCreatorFullname" name="creator" serialized-name="creator" groups="fullTask,partialTask"/>
<virtual-property method="getChangerFullname" name="changer" serialized-name="changer" groups="fullTask,partialTask"/>
</class>
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
<tag name="jms_serializer.event_subscriber"/>
</service>

<service id="sulu_automation.task.event_subscriber" class="Sulu\Bundle\AutomationBundle\EventSubscriber\PHPTaskEventSubscriber">
<argument type="service" id="request_stack"/>
<argument type="service" id="sulu.repository.task"/>

<tag name="kernel.event_subscriber"/>
</service>

<!-- FIXME hack to enable doctrine object constructor -->
<!--
could be fixed in serializer by changing JMS\SerializerBundle\DependencyInjection\Compiler\DoctrinePass:49
Expand Down
14 changes: 14 additions & 0 deletions Tasks/Model/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public function getTaskId();
*/
public function setTaskId($taskId);

/**
* Returns host.
*
* @return string
*/
public function getHost();

/**
* Returns scheme.
*
* @return string
*/
public function getScheme();

/**
* Returns creator full-name.
*
Expand Down
9 changes: 9 additions & 0 deletions Tasks/Model/TaskRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public function remove(TaskInterface $task);
*/
public function findById($id);

/**
* Find task-entity with given php-task id.
*
* @param int $id
*
* @return TaskInterface
*/
public function findByTaskId($id);

/**
* Count tasks which will be called in the future in given entity.
*
Expand Down
2 changes: 2 additions & 0 deletions Tests/Functional/Controller/TaskControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ public function testGetWithoutCreator()
$task->setLocale('de');
$task->setHandlerClass(FirstHandler::class);
$task->setSchedule(new \DateTime());
$task->setScheme('http');
$task->setHost('sulu.io');

$taskManager = $this->getContainer()->get('sulu_automation.tasks.manager');
$taskManager->create($task);
Expand Down
Loading