Skip to content

Commit

Permalink
OP-319 - Default parcel template and label type configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkalon committed Aug 13, 2024
1 parent e161c2e commit a317a37
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 20 deletions.
18 changes: 18 additions & 0 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,24 @@ By a standard, the `webpack.config.js` file should be available in your reposito
```


## Default parameters configuration
In the .env file, the default parcel size and label type can be specified by adding:

````
BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE='medium'
BITBAG_INPOST_DEFAULT_LABEL_TYPE='normal'
````

Three types of parcel templates are allowed:
- 'small'
- 'medium'
- 'large'

Two types of labels are allowed:
- 'normal'
- 'A6'


## Testing & running the plugin

```bash
Expand Down
5 changes: 4 additions & 1 deletion spec/Api/WebClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ final class WebClientSpec extends ObjectBehavior

public const LABEL_TYPE = "normal";

public const PARCEL_TEMPLATE = "medium";


public function let(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
): void
{
$this->beConstructedWith($client, $requestFactory, $streamFactory, self::LABEL_TYPE);
$this->beConstructedWith($client, $requestFactory, $streamFactory, self::LABEL_TYPE, self::PARCEL_TEMPLATE);
}

public function it_is_initializable(): void
Expand Down
3 changes: 2 additions & 1 deletion spec/EventListener/ShippingExportEventListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function let(
WebClientInterface $webClient,
InPostShippingExportActionProviderInterface $shippingExportActionProvider,
RequestStack $requestStack,
LoggerInterface $logger
LoggerInterface $logger,

): void {
$this->beConstructedWith($webClient, $shippingExportActionProvider, $requestStack, $logger);
}
Expand Down
20 changes: 9 additions & 11 deletions src/Api/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ final class WebClient implements WebClientInterface

private ShippingGatewayInterface $shippingGateway;

private ?ShippingExportInterface $shippingExport = null;
private ?string $labelType = null;

private string $labelType = 'normal';
private ?string $parcelTemplate = null;

public function __construct(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
string $labelType,
string $parcelTemplate,
) {
$this->apiClient = $client;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->labelType = $labelType;
$this->parcelTemplate = $parcelTemplate;
}

public function setShippingGateway(ShippingGatewayInterface $shippingGateway): WebClientInterface
Expand Down Expand Up @@ -154,10 +156,8 @@ public function getShipments(): ?array

public function createShipment(
ShipmentInterface $shipment,
?ShippingExportInterface $shippingExport = null,
ShippingExportInterface $shippingExport,
): array {
$this->shippingExport = $shippingExport;

/** @var OrderInterface $order */
$order = $shipment->getOrder();

Expand All @@ -168,7 +168,7 @@ public function createShipment(
'external_customer_id' => $customer->getId(),
'receiver' => $this->createReceiverDetails($order),
'custom_attributes' => $this->createCustomAttributes($shipment),
'parcels' => [$this->createParcel($shipment)],
'parcels' => [$this->createParcel($shipment, $shippingExport)],
'service' => $this->getShippingGatewayConfig('service'),
'additional_services' => $this->getAdditionalServices(),
'reference' => 'Order: ' . $order->getNumber(),
Expand Down Expand Up @@ -326,12 +326,10 @@ private function isCashOnDelivery(OrderInterface $order): bool

private function createParcel(
ShipmentInterface $shipment,
ShippingExportInterface $shippingExport,
): array {
$weight = $shipment->getShippingWeight();
$template = 'large';
if (null !== $this->shippingExport) {
$template = $this->shippingExport->getParcelTemplate();
}
$template = $shippingExport->getParcelTemplate();

return [
'id' => $shipment->getId(),
Expand All @@ -340,7 +338,7 @@ private function createParcel(
'unit' => 'kg',
],
'dimensions' => [],
'template' => $template,
'template' => $template ?? $this->parcelTemplate,
'tracking_number' => null,
'is_non_standard' => false,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Api/WebClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getLabels(array $shipmentIds): ?string;

public function getAuthorizedHeaderWithContentType(): array;

public function createShipment(ShipmentInterface $shipment, ?ShippingExportInterface $shippingExport = null): array;
public function createShipment(ShipmentInterface $shipment, ShippingExportInterface $shippingExport): array;

public function request(
string $method,
Expand Down
9 changes: 8 additions & 1 deletion src/Resources/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
parameters:
bitbag.inpost.label_type: 'normal'
bitbag.inpost.label_type: '%env(BITBAG_INPOST_DEFAULT_LABEL_TYPE)%'
bitbag.inpost.parcel_template: '%env(BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE)%'
env(BITBAG_INPOST_DEFAULT_LABEL_TYPE): 'normal'
env(BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE): 'large'

imports:
- { resource: "@BitBagSyliusInPostPlugin/Resources/config/services.xml" }
- { resource: "@BitBagSyliusInPostPlugin/Resources/config/resource/bitbag_inpost.yml" }
- { resource: "@BitBagSyliusInPostPlugin/Resources/config/sylius_ui.yml" }
- { resource: "@BitBagSyliusInPostPlugin/Resources/config/grid/*" }

twig:
globals:
inpost_client: '@bitbag.sylius_inpost_plugin.api.web_client'
bitbag_inpost_parcel_template: '%env(BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE)%'

1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<argument type="service" id="sylius.request_factory"/>
<argument type="service" id="sylius.stream_factory"/>
<argument>%bitbag.inpost.label_type%</argument>
<argument>%bitbag.inpost.parcel_template%</argument>
</service>

<service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{% 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>
{% set parcelTemplate = bitbag_inpost_parcel_template %}
{% endif %}

{% set templates = {} %}
Expand Down
3 changes: 3 additions & 0 deletions tests/Application/.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ APP_SECRET=EDITME
DATABASE_URL=mysql://[email protected]/bitbag_sylius_inpost_plugin_%kernel.environment%
###< doctrine/doctrine-bundle ###

BITBAG_INPOST_DEFAULT_LABEL_TYPE='normal'
BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE='medium'

###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
Expand Down

0 comments on commit a317a37

Please sign in to comment.