Skip to content

Commit

Permalink
Merge pull request #105 from rs2487/feature/variant-stock-query
Browse files Browse the repository at this point in the history
Refactored product variant gateway and added handler to load stock of…
  • Loading branch information
niklasnatter authored Sep 24, 2020
2 parents dd874ed + cb1a297 commit 0616ff7
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 35 deletions.
13 changes: 11 additions & 2 deletions Controller/Content/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

abstract class ContentController implements ClassResourceInterface
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public function getAction(Request $request, string $id): Response
{
$content = null;
try {
$message = new FindContentQuery($this->getResourceKey(), $id, $request->query->get('locale'));
$message = new FindContentQuery($this->getResourceKey(), $id, $this->getLocale($request));
$this->messageBus->dispatch($message);
$content = $message->getContent();
} catch (HandlerFailedException $exception) {
Expand All @@ -81,7 +82,7 @@ public function putAction(Request $request, string $resourceId): Response
'data' => $data,
];

$locale = $request->query->get('locale');
$locale = $this->getLocale($request);
$message = new ModifyContentMessage($this->getResourceKey(), $resourceId, $locale, $payload);
$this->messageBus->dispatch($message);

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

abstract protected function getResourceKey(): string;

public function getLocale(Request $request): string
{
$locale = $request->query->get('locale');
Assert::notNull($locale);

return $locale;
}
}
8 changes: 6 additions & 2 deletions Controller/Product/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

class ProductController implements ClassResourceInterface, SecuredControllerInterface
{
Expand Down Expand Up @@ -78,8 +79,11 @@ public function getSecurityContext()
return SyliusConsumerAdmin::PRODUCT_SECURITY_CONTEXT;
}

public function getLocale(Request $request)
public function getLocale(Request $request): string
{
return $request->query->get('locale');
$locale = $request->query->get('locale');
Assert::notNull($locale);

return $locale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sulu\Bundle\SyliusConsumerBundle\Gateway;

class ProductVariantChannelPricingGateway extends AbstractGateway implements ProductVariantChannelPricingGatewayInterface
class ProductVariantGateway extends AbstractGateway implements ProductVariantGatewayInterface
{
const URI = '/api/v1/products/{PRODUCT_ID}/variants/';

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

return $data['channelPricings'];
return json_decode($response->getBody()->getContents(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sulu\Bundle\SyliusConsumerBundle\Gateway;

interface ProductVariantChannelPricingGatewayInterface
interface ProductVariantGatewayInterface
{
public function findByCodeAndVariantCode(string $code, string $variantCode): array;
}
16 changes: 8 additions & 8 deletions Mail/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Sulu\Bundle\SyliusConsumerBundle\Mail;

use Sulu\Bundle\SyliusConsumerBundle\Model\Customer\CustomerInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;

class MailFactory
{
Expand All @@ -25,9 +25,9 @@ class MailFactory
protected $mailer;

/**
* @var EngineInterface
* @var Environment
*/
protected $engine;
protected $twig;

/**
* @var TranslatorInterface
Expand All @@ -41,12 +41,12 @@ class MailFactory

public function __construct(
\Swift_Mailer $mailer,
EngineInterface $engine,
Environment $twig,
TranslatorInterface $translator,
array $sender
) {
$this->mailer = $mailer;
$this->engine = $engine;
$this->twig = $twig;
$this->translator = $translator;
$this->sender = $sender;
}
Expand All @@ -60,7 +60,7 @@ public function sendVerifyEmail(CustomerInterface $customer): void
$this->sendEmail(
[$customer->getEmail() => $customer->getFullName()],
'sulu_sylius.email_customer_verify.subject',
'SuluSyliusConsumerBundle:Email:customer-verify.html.twig',
'@SuluSyliusConsumer/Email/customer-verify.html.twig',
[
'customer' => $customer,
'token' => $customer->getUser()->getToken(),
Expand All @@ -73,7 +73,7 @@ public function sendOrderConfirmationEmail(CustomerInterface $customer, array $o
$this->sendEmail(
[$customer->getEmail() => $customer->getFullName()],
'sulu_sylius.email_order-confirmation.subject',
'SuluSyliusConsumerBundle:Email:order-confirmation.html.twig',
'@SuluSyliusConsumer/Email/order-confirmation.html.twig',
[
'customer' => $customer,
'order' => $order,
Expand All @@ -97,7 +97,7 @@ protected function sendEmail(
$this->translator->setLocale($locale);
}

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

$message = new \Swift_Message();
$message->setSubject($this->translator->trans($subject));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@

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

use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface;
use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query\LoadProductVariantChannelPricingQuery;

class LoadProductVariantChannelPricingQueryHandler
{
/**
* @var ProductVariantChannelPricingGatewayInterface
* @var ProductVariantGatewayInterface
*/
private $gateway;

public function __construct(ProductVariantChannelPricingGatewayInterface $productChannelPricingGateway)
public function __construct(ProductVariantGatewayInterface $productVariantGateway)
{
$this->gateway = $productChannelPricingGateway;
$this->gateway = $productVariantGateway;
}

public function __invoke(LoadProductVariantChannelPricingQuery $query): void
{
$channelPricings = $this->gateway->findByCodeAndVariantCode($query->getCode(), $query->getVariantCode());
$variantData = $this->gateway->findByCodeAndVariantCode(
$query->getCode(),
$query->getVariantCode()
);

foreach ($channelPricings as $channelPricing) {
foreach ($variantData['channelPricings'] as $channelPricing) {
if ($query->getChannel() === $channelPricing['channelCode']) {
$query->setPrice($channelPricing['price']);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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

use Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query\LoadProductVariantStockQuery;

class LoadProductVariantStockQueryHandler
{
/**
* @var ProductVariantGatewayInterface
*/
private $gateway;

public function __construct(ProductVariantGatewayInterface $productVariantGateway)
{
$this->gateway = $productVariantGateway;
}

public function __invoke(LoadProductVariantStockQuery $query): void
{
$variantData = $this->gateway->findByCodeAndVariantCode(
$query->getCode(),
$query->getVariantCode()
);

$query->setOnHand($variantData['onHand']);
$query->setOnHold($variantData['onHold']);
}
}
77 changes: 77 additions & 0 deletions Model/Product/Query/LoadProductVariantStockQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\SyliusConsumerBundle\Model\Product\Query;

class LoadProductVariantStockQuery
{
/**
* @var string
*/
private $code;

/**
* @var string
*/
private $variantCode;

/**
* @var int|null
*/
private $onHold;

/**
* @var int|null
*/
private $onHand;

public function __construct(string $code, string $variantCode)
{
$this->code = $code;
$this->variantCode = $variantCode;
}

public function getCode(): string
{
return $this->code;
}

public function getVariantCode(): string
{
return $this->variantCode;
}

public function getOnHold(): ?int
{
return $this->onHold;
}

public function setOnHold(?int $onHold): self
{
$this->onHold = $onHold;

return $this;
}

public function getOnHand(): ?int
{
return $this->onHand;
}

public function setOnHand(?int $onHand): self
{
$this->onHand = $onHand;

return $this;
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

<service id="Sulu\Bundle\SyliusConsumerBundle\Mail\MailFactory">
<argument type="service" id="mailer"/>
<argument type="service" id="templating"/>
<argument type="service" id="twig"/>
<argument type="service" id="translator"/>
<argument>%sulu_sylius_consumer.mail_sender%</argument>
</service>
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services/gateway.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<argument type="service" id="sulu_sylius_consumer.gateway_client" />
</service>

<service id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface"
class="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGateway">
<service id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"
class="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGateway">
<argument type="service" id="sulu_sylius_consumer.gateway_client" />
</service>
</services>
Expand Down
8 changes: 7 additions & 1 deletion Resources/config/services/product-handler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@
</service>

<service id="Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query\LoadProductVariantChannelPricingQueryHandler">
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantChannelPricingGatewayInterface"/>
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"/>

<tag name="messenger.message_handler"/>
</service>

<service id="Sulu\Bundle\SyliusConsumerBundle\Model\Product\Handler\Query\LoadProductVariantStockQueryHandler">
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Gateway\ProductVariantGatewayInterface"/>

<tag name="messenger.message_handler"/>
</service>
Expand Down
4 changes: 2 additions & 2 deletions Resources/views/Email/customer-verify.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "SuluSyliusConsumerBundle:Email:master-email.html.twig" %}
{% extends "@SuluSyliusConsumer/Email/master-email.html.twig" %}

{% block content %}
{% set confirmUrl = url('sulu_sylius.customer.verify', { token: token }) %}
<a href="{{ confirmUrl }}">{{ confirmUrl }}</a>
{% endblock %}
{% endblock %}
2 changes: 1 addition & 1 deletion Resources/views/Email/order-confirmation.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "SuluSyliusConsumerBundle:Email:master-email.html.twig" %}
{% extends "@SuluSyliusConsumer/Email/master-email.html.twig" %}

{% block content %}
{{ 'sulu_sylius.email_order-confirmation.body'|trans({'%orderId%': order.id})|raw }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Model\Product\Handler;

use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Message\SynchronizeProductMessage;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\ProductInformation;
use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\DimensionTrait;
use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\ProductInformationTrait;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function testSynchronizeProductCreate()

$messageBus->dispatch($message);

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

Expand Down
Loading

0 comments on commit 0616ff7

Please sign in to comment.