Skip to content

Commit

Permalink
OP-319 - Extension of parcel template with courier services
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkalon committed Aug 8, 2024
1 parent 3cab4af commit dabaaca
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function it_should_throw_exception_if_incorrect_parcel_template_is_passed
public function it_should_perform_set_parcel_template_action(ResourceControllerEvent $event): void
{
$shippingGateway = ShippingGatewayBuilder::create()->withCode(self::INPOST)->build();
$shippingGateway->setConfig(['service' => 'inpost_locker_standard']);
$shipment = ShipmentBuilder::create()->build();
$shippingExport = ShippingExportBuilder::create()
->withShippingGateway($shippingGateway)
Expand Down
25 changes: 22 additions & 3 deletions src/EventListener/SelectParcelTemplateEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ class SelectParcelTemplateEventListener
{
private SelectParcelTemplateActionInterface $action;

private const PARCEL_TEMPLATE_VALUES = [
private const INPOST_LOCKER_PARCEL_TEMPLATES = [
'small', 'medium', 'large',
];

private const INPOST_COURIER_PARCEL_TEMPLATES = [
'small', 'medium', 'large', 'xlarge',
];

private const INPOST_LOCKER_STANDARD = 'inpost_locker_standard';

private const INPOST_LOCKER_PASS_THRU = 'inpost_locker_pass_thru';

private const ERROR_INVALID_PARCEL_TEMPLATE = '"%s" is an invalid parcel template!';

public function __construct(
SelectParcelTemplateActionInterface $action,
) {
Expand All @@ -36,8 +46,17 @@ public function setParcelTemplate(ResourceControllerEvent $exportShipmentEvent):
$shippingExport = $exportShipmentEvent->getSubject();
Assert::isInstanceOf($shippingExport, ShippingExportInterface::class);

if (!in_array($shippingExport->getParcelTemplate(), self::PARCEL_TEMPLATE_VALUES)) {
throw new \Exception(sprintf('"%s" is invalid parcel template!', $shippingExport->getParcelTemplate()));
$service = $shippingExport->getShippingGateway()->getConfig()['service'];
$parcelTemplate = $shippingExport->getParcelTemplate();

if (in_array($service, [self::INPOST_LOCKER_STANDARD, self::INPOST_LOCKER_PASS_THRU])) {
$validTemplates = self::INPOST_LOCKER_PARCEL_TEMPLATES;
} else {
$validTemplates = self::INPOST_COURIER_PARCEL_TEMPLATES;
}

if (!in_array($parcelTemplate, $validTemplates, true)) {
throw new \Exception(sprintf(self::ERROR_INVALID_PARCEL_TEMPLATE, $parcelTemplate));
}

$this->action->execute($shippingExport);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% set parcelTemplate = data.getParcelTemplate %}

{% if parcelTemplate is null %}
<span class="ui label">
{{ 'bitbag_sylius_inpost_plugin.ui.parcel_template.select_template'|trans }}
</span>
<div class="ui divider"></div>
{% endif %}

{% set templates = {} %}
{% if data.getShippingGateway.getConfig.service == 'inpost_locker_standard' %}
{% set templates = {'small': 'A', 'medium': 'B', 'large': 'C'} %}
{% else %}
{% set templates = {'small': 'A', 'medium': 'B', 'large': 'C', 'xlarge': 'D'} %}
{% endif %}

<div class="ui buttons">
{% for key, label in templates %}
{% if parcelTemplate == key %}
<button class="ui button primary">{{ label }}</button>
{% else %}
{% if data.getExportedAt != null %}
<button class="ui button disabled">{{ label }}</button>
{% else %}
<form action="{{ path('bitbag_admin_shipping_export_select_parcel_template', {'id' : data.id, 'template': key}) }}"
method="POST">
<input type="hidden" name="_method" value="PUT">
<button class="ui button shipping-export-select-template-button"
data-content="The default theme's basic popup removes the pointing arrow."
data-variation="basic">
{{ label }}
</button>
</form>
{% endif %}
{% endif %}
{% endfor %}
</div>
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
{% set template = data.getParcelTemplate %}
{% if template is null %}
<span class="ui label">
{{ 'bitbag_sylius_inpost_plugin.ui.parcel_template.select_template'|trans }}
</span>
<div class="ui divider"></div>
{% if data.getShippingGateway.getName == 'INPOST_PL' %}
{% include '@BitBagSyliusInPostPlugin/ShippingExport/Grid/Field/ParcelTemplate/_inpost_template.html.twig' %}
{% else %}
<i class="minus icon"></i>
{% endif %}

<div class="ui buttons">
{% set templates = {'small': 'A', 'medium': 'B', 'large': 'C'} %}
{% for key, label in templates %}
{% if template == key %}
<button class="ui button primary">{{ label }}</button>
{% else %}
{% if data.getExportedAt != null %}
<button class="ui button disabled">{{ label }}</button>
{% else %}
<form action="{{ path('bitbag_admin_shipping_export_select_parcel_template', {'id' : data.id, 'template': key}) }}"
method="POST">
<input type="hidden" name="_method" value="PUT">
<button class="ui button">{{ label }}</button>
</form>
{% endif %}
{% endif %}
{% endfor %}
</div>

0 comments on commit dabaaca

Please sign in to comment.