Skip to content

Commit

Permalink
Create an upload order request upon order completion
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 14, 2024
1 parent 09265ea commit 4c7a80d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 11 deletions.
41 changes: 41 additions & 0 deletions src/EventSubscriber/CreateUploadOrderRequestSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\EventSubscriber;

use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;
use Setono\SyliusPeakWMSPlugin\Factory\UploadOrderRequestFactoryInterface;
use Setono\SyliusPeakWMSPlugin\Model\OrderInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Webmozart\Assert\Assert;

final class CreateUploadOrderRequestSubscriber implements EventSubscriberInterface
{
use ORMTrait;

public function __construct(
ManagerRegistry $managerRegistry,
private readonly UploadOrderRequestFactoryInterface $uploadOrderRequestFactory,
) {
$this->managerRegistry = $managerRegistry;
}

public static function getSubscribedEvents(): array
{
return [
'sylius.order.pre_complete' => 'createUploadOrderRequest',
];
}

public function createUploadOrderRequest(ResourceControllerEvent $event): void
{
/** @var OrderInterface|mixed $order */
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);

$order->setPeakWMSUploadOrderRequest($this->uploadOrderRequestFactory->createNew());

Check failure on line 39 in src/EventSubscriber/CreateUploadOrderRequestSubscriber.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

MixedMethodCall

src/EventSubscriber/CreateUploadOrderRequestSubscriber.php:39:17: MixedMethodCall: Cannot determine the type of $order when calling method setPeakWMSUploadOrderRequest (see https://psalm.dev/015)

Check failure on line 39 in src/EventSubscriber/CreateUploadOrderRequestSubscriber.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~6.4.0)

MixedMethodCall

src/EventSubscriber/CreateUploadOrderRequestSubscriber.php:39:17: MixedMethodCall: Cannot determine the type of $order when calling method setPeakWMSUploadOrderRequest (see https://psalm.dev/015)

Check failure on line 39 in src/EventSubscriber/CreateUploadOrderRequestSubscriber.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)

MixedMethodCall

src/EventSubscriber/CreateUploadOrderRequestSubscriber.php:39:17: MixedMethodCall: Cannot determine the type of $order when calling method setPeakWMSUploadOrderRequest (see https://psalm.dev/015)
}
}
22 changes: 22 additions & 0 deletions src/Factory/UploadOrderRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Factory;

use Setono\SyliusPeakWMSPlugin\Model\UploadOrderRequestInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class UploadOrderRequestFactory implements UploadOrderRequestFactoryInterface
{
public function __construct(
/** @var FactoryInterface<UploadOrderRequestInterface> $decoratedFactory */
private readonly FactoryInterface $decoratedFactory,
) {
}

public function createNew(): UploadOrderRequestInterface
{
return $this->decoratedFactory->createNew();
}
}
16 changes: 16 additions & 0 deletions src/Factory/UploadOrderRequestFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Factory;

use Setono\SyliusPeakWMSPlugin\Model\UploadOrderRequestInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

/**
* @extends FactoryInterface<UploadOrderRequestInterface>
*/
interface UploadOrderRequestFactoryInterface extends FactoryInterface
{
public function createNew(): UploadOrderRequestInterface;
}
9 changes: 2 additions & 7 deletions src/Model/OrderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

trait OrderTrait
{
/**
* @ORM\OneToOne(inversedBy="order", targetEntity="Setono\SyliusPeakWMSPlugin\Model\UploadOrderRequestInterface", cascade={"persist"}, orphanRemoval=true)
*
* @ORM\JoinColumn(name="peak_wms_upload_order_request_id", referencedColumnName="id", unique=true, nullable=true, onDelete="SET NULL")
*/
#[ORM\OneToOne(inversedBy: 'order', targetEntity: UploadOrderRequestInterface::class, cascade: ['persist'], orphanRemoval: true)]
#[ORM\JoinColumn(name: 'peak_wms_upload_order_request_id', referencedColumnName: 'id', unique: true, nullable: true, onDelete: 'SET NULL')]
/** @ORM\OneToOne(mappedBy="order", targetEntity="Setono\SyliusPeakWMSPlugin\Model\UploadOrderRequestInterface", cascade={"persist"}, orphanRemoval=true) */
#[ORM\OneToOne(mappedBy: 'order', targetEntity: UploadOrderRequestInterface::class, cascade: ['persist'], orphanRemoval: true)]
protected ?UploadOrderRequestInterface $peakWMSUploadOrderRequest = null;

public function getPeakWMSUploadOrderRequest(): ?UploadOrderRequestInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Model/UploadOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getVersion(): int

public function setVersion(?int $version): void
{
$this->version = $version;
$this->version = (int) $version;
}

public function getState(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<field name="version" type="integer" version="true"/>
<field name="state" type="string"/>

<one-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface" mapped-by="peakWMSUploadOrderRequest">
<one-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface" inversed-by="peakWMSUploadOrderRequest">
<join-column name="order_id" referenced-column-name="id" nullable="false" unique="true" on-delete="CASCADE"/>
</one-to-one>
</mapped-superclass>
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_peak_wms.event_subscriber.grid.add_upload_order_request_column_to_order_grid"
class="Setono\SyliusPeakWMSPlugin\EventSubscriber\Grid\AddUploadOrderRequestColumnToOrderGridSubscriber">
<tag name="kernel.event_subscriber"/>
</service>

<service id="setono_sylius_peak_wms.event_subscriber.add_menu"
class="Setono\SyliusPeakWMSPlugin\EventSubscriber\AddMenuSubscriber">
<tag name="kernel.event_subscriber"/>
</service>

<service id="setono_sylius_peak_wms.event_subscriber.grid.add_upload_order_request_column_to_order_grid"
class="Setono\SyliusPeakWMSPlugin\EventSubscriber\Grid\AddUploadOrderRequestColumnToOrderGridSubscriber">
<service id="setono_sylius_peak_wms.event_subscriber.create_upload_order_request"
class="Setono\SyliusPeakWMSPlugin\EventSubscriber\CreateUploadOrderRequestSubscriber">
<argument type="service" id="doctrine"/>
<argument type="service" id="setono_sylius_peak_wms.factory.upload_order_request"/>

<tag name="kernel.event_subscriber"/>
</service>
</services>
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
decorates="setono_sylius_peak_wms.factory.remote_event" decoration-priority="64">
<argument type="service" id="setono_sylius_peak_wms.custom_factory.remote_event.inner"/>
</service>

<service id="setono_sylius_peak_wms.custom_factory.upload_order_request"
class="Setono\SyliusPeakWMSPlugin\Factory\UploadOrderRequestFactory"
decorates="setono_sylius_peak_wms.factory.upload_order_request" decoration-priority="64">
<argument type="service" id="setono_sylius_peak_wms.custom_factory.upload_order_request.inner"/>
</service>
</services>
</container>

0 comments on commit 4c7a80d

Please sign in to comment.