Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored product variant gateway and added handler to load stock of… #105

Merged
merged 2 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Controller/Content/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 +81,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 +107,9 @@ 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)
{
return $request->query->get('locale') ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'' is a strange fallback here. this could lead to confusing behaviour. i think i would expect an error here if no locale is given

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return $request->query->get('locale') ?? '';
$locale = $request->query->get('locale');
Assert::string($locale);
return $locale;

}
}
2 changes: 1 addition & 1 deletion Controller/Product/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ public function getSecurityContext()

public function getLocale(Request $request)
{
return $request->query->get('locale');
return $request->query->get('locale') ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}
}
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
17 changes: 13 additions & 4 deletions Tests/Functional/Traits/ProductInformationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sulu\Bundle\SyliusConsumerBundle\Model\Dimension\DimensionInterface;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Product;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\ProductInformation;
use Sulu\Bundle\SyliusConsumerBundle\Model\Product\ProductInterface;

trait ProductInformationTrait
{
Expand All @@ -29,8 +30,9 @@ protected function createProductInformation(string $productId, string $locale):
]
);

/** @var ProductInterface $product */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the reason for this? also i dont think that type is right, cant this be null too? 🤔

$product = $this->getEntityManager()->find(Product::class, $productId);
if (!$product) {
if (!$product instanceof ProductInterface) {
throw new \RuntimeException('Product not fount');
}

Expand All @@ -52,8 +54,9 @@ protected function createProductInformationLive(string $productId, string $local
]
);

/** @var ProductInterface $product */
$product = $this->getEntityManager()->find(Product::class, $productId);
if (!$product) {
if (!$product instanceof ProductInterface) {
throw new \RuntimeException('Product not fount');
}

Expand All @@ -75,10 +78,13 @@ protected function findProductInformation(string $id, string $locale): ?ProductI
]
);

return $this->getEntityManager()->find(
/** @var ProductInformation $productInformation */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var ProductInformation $productInformation */
/** @var ProductInformation|null $productInformation */

$productInformation = $this->getEntityManager()->find(
ProductInformation::class,
['product' => $id, 'dimension' => $dimension]
);

return $productInformation;
}

protected function findProductInformationByCode(string $code, string $locale): ?ProductInformation
Expand All @@ -95,13 +101,16 @@ protected function findProductInformationByCode(string $code, string $locale): ?
throw new \RuntimeException('Product not fount');
}

return $this->getEntityManager()->find(
/** @var ProductInformation $productInformation */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var ProductInformation $productInformation */
/** @var ProductInformation|null $productInformation */

$productInformation = $this->getEntityManager()->find(
ProductInformation::class,
[
'product' => $product,
'dimension' => $dimension,
]
);

return $productInformation;
}

abstract protected function findDimension(array $attributes): DimensionInterface;
Expand Down
Loading