Skip to content

Commit 0616ff7

Browse files
authored
Merge pull request #105 from rs2487/feature/variant-stock-query
Refactored product variant gateway and added handler to load stock of…
2 parents dd874ed + cb1a297 commit 0616ff7

18 files changed

+190
-35
lines changed

Controller/Content/ContentController.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpFoundation\Response;
2424
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2525
use Symfony\Component\Messenger\MessageBusInterface;
26+
use Webmozart\Assert\Assert;
2627

2728
abstract class ContentController implements ClassResourceInterface
2829
{
@@ -58,7 +59,7 @@ public function getAction(Request $request, string $id): Response
5859
{
5960
$content = null;
6061
try {
61-
$message = new FindContentQuery($this->getResourceKey(), $id, $request->query->get('locale'));
62+
$message = new FindContentQuery($this->getResourceKey(), $id, $this->getLocale($request));
6263
$this->messageBus->dispatch($message);
6364
$content = $message->getContent();
6465
} catch (HandlerFailedException $exception) {
@@ -81,7 +82,7 @@ public function putAction(Request $request, string $resourceId): Response
8182
'data' => $data,
8283
];
8384

84-
$locale = $request->query->get('locale');
85+
$locale = $this->getLocale($request);
8586
$message = new ModifyContentMessage($this->getResourceKey(), $resourceId, $locale, $payload);
8687
$this->messageBus->dispatch($message);
8788

@@ -107,4 +108,12 @@ protected function handleAction(string $resourceId, string $locale, string $acti
107108
abstract protected function handlePublish(string $resourceId, string $locale): void;
108109

109110
abstract protected function getResourceKey(): string;
111+
112+
public function getLocale(Request $request): string
113+
{
114+
$locale = $request->query->get('locale');
115+
Assert::notNull($locale);
116+
117+
return $locale;
118+
}
110119
}

Controller/Product/ProductController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\HttpFoundation\Request;
2525
use Symfony\Component\HttpFoundation\Response;
2626
use Symfony\Component\Messenger\MessageBusInterface;
27+
use Webmozart\Assert\Assert;
2728

2829
class ProductController implements ClassResourceInterface, SecuredControllerInterface
2930
{
@@ -78,8 +79,11 @@ public function getSecurityContext()
7879
return SyliusConsumerAdmin::PRODUCT_SECURITY_CONTEXT;
7980
}
8081

81-
public function getLocale(Request $request)
82+
public function getLocale(Request $request): string
8283
{
83-
return $request->query->get('locale');
84+
$locale = $request->query->get('locale');
85+
Assert::notNull($locale);
86+
87+
return $locale;
8488
}
8589
}

Gateway/ProductVariantChannelPricingGateway.php Gateway/ProductVariantGateway.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Sulu\Bundle\SyliusConsumerBundle\Gateway;
1515

16-
class ProductVariantChannelPricingGateway extends AbstractGateway implements ProductVariantChannelPricingGatewayInterface
16+
class ProductVariantGateway extends AbstractGateway implements ProductVariantGatewayInterface
1717
{
1818
const URI = '/api/v1/products/{PRODUCT_ID}/variants/';
1919

@@ -27,8 +27,7 @@ public function findByCodeAndVariantCode(string $code, string $variantCode): arr
2727
if (200 !== $response->getStatusCode()) {
2828
$this->handleErrors($response);
2929
}
30-
$data = json_decode($response->getBody()->getContents(), true);
3130

32-
return $data['channelPricings'];
31+
return json_decode($response->getBody()->getContents(), true);
3332
}
3433
}

Gateway/ProductVariantChannelPricingGatewayInterface.php Gateway/ProductVariantGatewayInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Sulu\Bundle\SyliusConsumerBundle\Gateway;
1515

16-
interface ProductVariantChannelPricingGatewayInterface
16+
interface ProductVariantGatewayInterface
1717
{
1818
public function findByCodeAndVariantCode(string $code, string $variantCode): array;
1919
}

Mail/MailFactory.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
namespace Sulu\Bundle\SyliusConsumerBundle\Mail;
1515

1616
use Sulu\Bundle\SyliusConsumerBundle\Model\Customer\CustomerInterface;
17-
use Symfony\Component\Templating\EngineInterface;
1817
use Symfony\Component\Translation\TranslatorInterface;
18+
use Twig\Environment;
1919

2020
class MailFactory
2121
{
@@ -25,9 +25,9 @@ class MailFactory
2525
protected $mailer;
2626

2727
/**
28-
* @var EngineInterface
28+
* @var Environment
2929
*/
30-
protected $engine;
30+
protected $twig;
3131

3232
/**
3333
* @var TranslatorInterface
@@ -41,12 +41,12 @@ class MailFactory
4141

4242
public function __construct(
4343
\Swift_Mailer $mailer,
44-
EngineInterface $engine,
44+
Environment $twig,
4545
TranslatorInterface $translator,
4646
array $sender
4747
) {
4848
$this->mailer = $mailer;
49-
$this->engine = $engine;
49+
$this->twig = $twig;
5050
$this->translator = $translator;
5151
$this->sender = $sender;
5252
}
@@ -60,7 +60,7 @@ public function sendVerifyEmail(CustomerInterface $customer): void
6060
$this->sendEmail(
6161
[$customer->getEmail() => $customer->getFullName()],
6262
'sulu_sylius.email_customer_verify.subject',
63-
'SuluSyliusConsumerBundle:Email:customer-verify.html.twig',
63+
'@SuluSyliusConsumer/Email/customer-verify.html.twig',
6464
[
6565
'customer' => $customer,
6666
'token' => $customer->getUser()->getToken(),
@@ -73,7 +73,7 @@ public function sendOrderConfirmationEmail(CustomerInterface $customer, array $o
7373
$this->sendEmail(
7474
[$customer->getEmail() => $customer->getFullName()],
7575
'sulu_sylius.email_order-confirmation.subject',
76-
'SuluSyliusConsumerBundle:Email:order-confirmation.html.twig',
76+
'@SuluSyliusConsumer/Email/order-confirmation.html.twig',
7777
[
7878
'customer' => $customer,
7979
'order' => $order,
@@ -97,7 +97,7 @@ protected function sendEmail(
9797
$this->translator->setLocale($locale);
9898
}
9999

100-
$body = $this->engine->render($template, $data);
100+
$body = $this->twig->render($template, $data);
101101

102102
$message = new \Swift_Message();
103103
$message->setSubject($this->translator->trans($subject));

Model/Product/Handler/Query/LoadProductVariantChannelPricingQueryHandler.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,29 @@
1313

1414
namespace Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query;
1515

16-
use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface;
16+
use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface;
1717
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query\LoadProductVariantChannelPricingQuery;
1818

1919
class LoadProductVariantChannelPricingQueryHandler
2020
{
2121
/**
22-
* @var ProductVariantChannelPricingGatewayInterface
22+
* @var ProductVariantGatewayInterface
2323
*/
2424
private $gateway;
2525

26-
public function __construct(ProductVariantChannelPricingGatewayInterface $productChannelPricingGateway)
26+
public function __construct(ProductVariantGatewayInterface $productVariantGateway)
2727
{
28-
$this->gateway = $productChannelPricingGateway;
28+
$this->gateway = $productVariantGateway;
2929
}
3030

3131
public function __invoke(LoadProductVariantChannelPricingQuery $query): void
3232
{
33-
$channelPricings = $this->gateway->findByCodeAndVariantCode($query->getCode(), $query->getVariantCode());
33+
$variantData = $this->gateway->findByCodeAndVariantCode(
34+
$query->getCode(),
35+
$query->getVariantCode()
36+
);
3437

35-
foreach ($channelPricings as $channelPricing) {
38+
foreach ($variantData['channelPricings'] as $channelPricing) {
3639
if ($query->getChannel() === $channelPricing['channelCode']) {
3740
$query->setPrice($channelPricing['price']);
3841
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) MASSIVE ART WebServices GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query;
15+
16+
use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface;
17+
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query\LoadProductVariantStockQuery;
18+
19+
class LoadProductVariantStockQueryHandler
20+
{
21+
/**
22+
* @var ProductVariantGatewayInterface
23+
*/
24+
private $gateway;
25+
26+
public function __construct(ProductVariantGatewayInterface $productVariantGateway)
27+
{
28+
$this->gateway = $productVariantGateway;
29+
}
30+
31+
public function __invoke(LoadProductVariantStockQuery $query): void
32+
{
33+
$variantData = $this->gateway->findByCodeAndVariantCode(
34+
$query->getCode(),
35+
$query->getVariantCode()
36+
);
37+
38+
$query->setOnHand($variantData['onHand']);
39+
$query->setOnHold($variantData['onHold']);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) MASSIVE ART WebServices GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query;
15+
16+
class LoadProductVariantStockQuery
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $code;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $variantCode;
27+
28+
/**
29+
* @var int|null
30+
*/
31+
private $onHold;
32+
33+
/**
34+
* @var int|null
35+
*/
36+
private $onHand;
37+
38+
public function __construct(string $code, string $variantCode)
39+
{
40+
$this->code = $code;
41+
$this->variantCode = $variantCode;
42+
}
43+
44+
public function getCode(): string
45+
{
46+
return $this->code;
47+
}
48+
49+
public function getVariantCode(): string
50+
{
51+
return $this->variantCode;
52+
}
53+
54+
public function getOnHold(): ?int
55+
{
56+
return $this->onHold;
57+
}
58+
59+
public function setOnHold(?int $onHold): self
60+
{
61+
$this->onHold = $onHold;
62+
63+
return $this;
64+
}
65+
66+
public function getOnHand(): ?int
67+
{
68+
return $this->onHand;
69+
}
70+
71+
public function setOnHand(?int $onHand): self
72+
{
73+
$this->onHand = $onHand;
74+
75+
return $this;
76+
}
77+
}

Resources/config/services.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
<service id="Sulu\Bundle\SyliusConsumerBundle\Mail\MailFactory">
7474
<argument type="service" id="mailer"/>
75-
<argument type="service" id="templating"/>
75+
<argument type="service" id="twig"/>
7676
<argument type="service" id="translator"/>
7777
<argument>%sulu_sylius_consumer.mail_sender%</argument>
7878
</service>

Resources/config/services/gateway.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<argument type="service" id="sulu_sylius_consumer.gateway_client" />
3434
</service>
3535

36-
<service id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface"
37-
class="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGateway">
36+
<service id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"
37+
class="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGateway">
3838
<argument type="service" id="sulu_sylius_consumer.gateway_client" />
3939
</service>
4040
</services>

Resources/config/services/product-handler.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@
138138
</service>
139139

140140
<service id="Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query\LoadProductVariantChannelPricingQueryHandler">
141-
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface"/>
141+
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"/>
142+
143+
<tag name="messenger.message_handler"/>
144+
</service>
145+
146+
<service id="Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query\LoadProductVariantStockQueryHandler">
147+
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"/>
142148

143149
<tag name="messenger.message_handler"/>
144150
</service>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{% extends "SuluSyliusConsumerBundle:Email:master-email.html.twig" %}
1+
{% extends "@SuluSyliusConsumer/Email/master-email.html.twig" %}
22

33
{% block content %}
44
{% set confirmUrl = url('sulu_sylius.customer.verify', { token: token }) %}
55
<a href="{{ confirmUrl }}">{{ confirmUrl }}</a>
6-
{% endblock %}
6+
{% endblock %}

Resources/views/Email/order-confirmation.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "SuluSyliusConsumerBundle:Email:master-email.html.twig" %}
1+
{% extends "@SuluSyliusConsumer/Email/master-email.html.twig" %}
22

33
{% block content %}
44
{{ 'sulu_sylius.email_order-confirmation.body'|trans({'%orderId%': order.id})|raw }}

Tests/Functional/Model/Product/Handler/SynchronizeProductTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Model\Product\Handler;
1515

1616
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Message\SynchronizeProductMessage;
17+
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\ProductInformation;
1718
use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\DimensionTrait;
1819
use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\ProductInformationTrait;
1920
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
@@ -41,6 +42,7 @@ public function testSynchronizeProductCreate()
4142

4243
$messageBus->dispatch($message);
4344

45+
/** @var ProductInformation|null $result */
4446
$result = $this->findProductInformationByCode(ExampleSynchronizeProductMessage::getCode(), 'de');
4547
$this->assertNotNull($result);
4648

0 commit comments

Comments
 (0)