Skip to content

Commit

Permalink
Add reset upload order request link
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Sep 9, 2024
1 parent 5e56de6 commit 1dcf9f6
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 6 deletions.
62 changes: 62 additions & 0 deletions src/Controller/Admin/ResetUploadOrderRequestAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\Controller\Admin;

use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;
use Setono\SyliusPeakPlugin\Model\UploadOrderRequestInterface;
use Setono\SyliusPeakPlugin\Workflow\UploadOrderRequestWorkflow;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\WorkflowInterface;

final class ResetUploadOrderRequestAction
{
use ORMTrait;

public function __construct(

Check warning on line 22 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L22

Added line #L22 was not covered by tests
ManagerRegistry $managerRegistry,
private readonly WorkflowInterface $uploadOrderRequestWorkflow,
private readonly UrlGeneratorInterface $urlGenerator,
/** @var class-string<UploadOrderRequestInterface> $uploadOrderRequestClass */
private readonly string $uploadOrderRequestClass,
) {
$this->managerRegistry = $managerRegistry;

Check warning on line 29 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L29

Added line #L29 was not covered by tests
}

public function __invoke(Request $request, int $id): RedirectResponse

Check warning on line 32 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L32

Added line #L32 was not covered by tests
{
$manager = $this->getManager($this->uploadOrderRequestClass);

Check warning on line 34 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L34

Added line #L34 was not covered by tests

/** @var UploadOrderRequestInterface|null $uploadOrderRequest */
$uploadOrderRequest = $manager->find($this->uploadOrderRequestClass, $id);
if ($uploadOrderRequest === null) {
throw new NotFoundHttpException(sprintf('Upload order request with id %d not found', $id));

Check warning on line 39 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L37-L39

Added lines #L37 - L39 were not covered by tests
}

if ($this->uploadOrderRequestWorkflow->can($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_RESET)) {
$this->uploadOrderRequestWorkflow->apply($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_RESET);

Check warning on line 43 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L42-L43

Added lines #L42 - L43 were not covered by tests
}

$manager->flush();

Check warning on line 46 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L46

Added line #L46 was not covered by tests

$session = $request->getSession();
if ($session instanceof Session) {
$session->getFlashBag()->add('success', 'setono_sylius_peak.upload_order_request_reset');

Check warning on line 50 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L48-L50

Added lines #L48 - L50 were not covered by tests
}

$referrer = $request->headers->get('referer');
if (is_string($referrer)) {
return new RedirectResponse($referrer);

Check warning on line 55 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L53-L55

Added lines #L53 - L55 were not covered by tests
}

return new RedirectResponse($this->urlGenerator->generate('sylius_admin_order_show', [
'id' => $uploadOrderRequest->getOrder()?->getId(),
]));

Check warning on line 60 in src/Controller/Admin/ResetUploadOrderRequestAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Admin/ResetUploadOrderRequestAction.php#L58-L60

Added lines #L58 - L60 were not covered by tests
}
}
3 changes: 2 additions & 1 deletion src/EventSubscriber/AddLinkToPeakSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function addLink(OrderShowMenuBuilderEvent $event): void
}

$menu = $event->getMenu();
$sort = array_keys($menu->getChildren());

Check warning on line 45 in src/EventSubscriber/AddLinkToPeakSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddLinkToPeakSubscriber.php#L45

Added line #L45 was not covered by tests

$menu
->addChild(self::MENU_ITEM_KEY, [
'uri' => sprintf('https://app%s.peakwms.com/dialog/orderOverview/%d/details', $this->testEnvironment ? '-test' : '', $peakOrderId),
Expand All @@ -52,7 +54,6 @@ public function addLink(OrderShowMenuBuilderEvent $event): void
->setLabelAttribute('color', 'blue')
;

$sort = array_keys($menu->getChildren());
array_unshift($sort, self::MENU_ITEM_KEY);

try {
Expand Down
63 changes: 63 additions & 0 deletions src/EventSubscriber/AddResetLinkSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\EventSubscriber;

use Setono\SyliusPeakPlugin\Model\OrderInterface;
use Setono\SyliusPeakPlugin\Workflow\UploadOrderRequestWorkflow;
use Sylius\Bundle\AdminBundle\Event\OrderShowMenuBuilderEvent;
use Sylius\Bundle\AdminBundle\Menu\OrderShowMenuBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\WorkflowInterface;

final class AddResetLinkSubscriber implements EventSubscriberInterface
{
private const MENU_ITEM_KEY = 'reset_upload_order_request';

public function __construct(private readonly WorkflowInterface $workflow)

Check warning on line 18 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L18

Added line #L18 was not covered by tests
{
}

public static function getSubscribedEvents(): array

Check warning on line 22 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L22

Added line #L22 was not covered by tests
{
return [OrderShowMenuBuilder::EVENT_NAME => 'addLink'];

Check warning on line 24 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L24

Added line #L24 was not covered by tests
}

public function addLink(OrderShowMenuBuilderEvent $event): void

Check warning on line 27 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L27

Added line #L27 was not covered by tests
{
$order = $event->getOrder();
if (!$order instanceof OrderInterface) {
return;

Check warning on line 31 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L29-L31

Added lines #L29 - L31 were not covered by tests
}

$uploadOrderRequest = $order->getPeakUploadOrderRequest();
if ($uploadOrderRequest === null) {
return;

Check warning on line 36 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L34-L36

Added lines #L34 - L36 were not covered by tests
}

if (!$this->workflow->can($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_RESET)) {
return;

Check warning on line 40 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L39-L40

Added lines #L39 - L40 were not covered by tests
}

$menu = $event->getMenu();
$sort = array_keys($menu->getChildren());

Check warning on line 44 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L43-L44

Added lines #L43 - L44 were not covered by tests

$menu
->addChild(self::MENU_ITEM_KEY, [
'route' => 'setono_sylius_peak_admin_reset_upload_order_request',
'routeParameters' => ['id' => $uploadOrderRequest->getId()],
])
->setAttribute('type', 'link')
->setLabel($uploadOrderRequest->getPeakOrderId() === null ? 'setono_sylius_peak.ui.upload_order_to_peak' : 'setono_sylius_peak.ui.re_upload_order_to_peak')
->setLabelAttribute('icon', 'redo')
;

Check warning on line 54 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L46-L54

Added lines #L46 - L54 were not covered by tests

array_unshift($sort, self::MENU_ITEM_KEY);

Check warning on line 56 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L56

Added line #L56 was not covered by tests

try {
$event->getMenu()->reorderChildren($sort);
} catch (\InvalidArgumentException) {

Check warning on line 60 in src/EventSubscriber/AddResetLinkSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/EventSubscriber/AddResetLinkSubscriber.php#L59-L60

Added lines #L59 - L60 were not covered by tests
}
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ setono_sylius_peak_admin_peak_register_webhooks:
methods: [GET]
defaults:
_controller: Setono\SyliusPeakPlugin\Controller\Admin\PeakController::registerWebhooks

setono_sylius_peak_admin_reset_upload_order_request:
path: /peak/upload-order-requests/{id}/reset
methods: [GET]
defaults:
_controller: Setono\SyliusPeakPlugin\Controller\Admin\ResetUploadOrderRequestAction
7 changes: 7 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<tag name="controller.service_arguments"/>
</service>

<service id="Setono\SyliusPeakPlugin\Controller\Admin\ResetUploadOrderRequestAction" public="true">
<argument type="service" id="doctrine"/>
<argument type="service" id="state_machine.setono_sylius_peak__upload_order_request"/>
<argument type="service" id="router"/>
<argument>%setono_sylius_peak.model.upload_order_request.class%</argument>
</service>

<service id="Setono\SyliusPeakPlugin\Controller\HandleWebhookAction" public="true">
<argument type="service" id="Setono\PeakWMS\Parser\WebhookParserInterface"/>
<argument type="service" id="Setono\SyliusPeakPlugin\WebhookHandler\WebhookHandlerInterface"/>
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="Setono\SyliusPeakPlugin\EventSubscriber\AddResetLinkSubscriber">
<argument type="service" id="state_machine.setono_sylius_peak__upload_order_request"/>

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

<service id="Setono\SyliusPeakPlugin\EventSubscriber\CreateUploadOrderRequestSubscriber">
<argument type="service" id="doctrine"/>
<argument type="service" id="setono_sylius_peak.factory.upload_order_request"/>
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/flashes.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setono_sylius_peak:
upload_order_request_reset: The request to upload the order has been created successfully.
10 changes: 6 additions & 4 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
setono_sylius_peak:
ui:
failed: Failed
pending: Pending
register_webhooks: Register webhooks
register_webhooks_information: Your webhooks registration is out of date. Please click the button to register webhooks with Peak.
peak: Peak
peak_index: Manage your Peak settings
peak_state: Peak state
uploaded: Uploaded
pending: Pending
processing: Processing
re_upload_order_to_peak: Re-upload order to Peak
register_webhooks: Register webhooks
register_webhooks_information: Your webhooks registration is out of date. Please click the button to register webhooks with Peak.
upload_order_to_peak: Upload order to Peak
uploaded: Uploaded
view_order_in_peak: View order in Peak
webhooks: Webhooks
2 changes: 1 addition & 1 deletion src/Workflow/UploadOrderRequestWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function getTransitions(): array
),
new Transition(
self::TRANSITION_RESET,
[UploadOrderRequestInterface::STATE_FAILED, UploadOrderRequestInterface::STATE_UPLOADED],
[UploadOrderRequestInterface::STATE_PENDING, UploadOrderRequestInterface::STATE_FAILED, UploadOrderRequestInterface::STATE_UPLOADED],
UploadOrderRequestInterface::STATE_PENDING,
),
];
Expand Down

0 comments on commit 1dcf9f6

Please sign in to comment.