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

[API]Add inStock serialization to productVariant #12639

Merged
merged 2 commits into from
May 17, 2021
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
1 change: 1 addition & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<argument type="service" id="api_platform.serializer.normalizer.item" />
<argument type="service" id="sylius.calculator.product_variant_price" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.availability_checker" />
<tag name="serializer.normalizer" priority="64" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface;
use Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Webmozart\Assert\Assert;
Expand All @@ -17,30 +18,36 @@ final class ProductVariantSerializer implements ContextAwareNormalizerInterface
/** @var NormalizerInterface */
private $objectNormalizer;

/** @var ProductVariantPriceCalculatorInterface */
/** @var ProductVariantPricesCalculatorInterface */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this Interface, as It shows as deprecated.

private $priceCalculator;

/** @var ChannelContextInterface */
private $channelContext;

/** @var AvailabilityCheckerInterface */
private $availabilityChecker;

public function __construct(
NormalizerInterface $objectNormalizer,
ProductVariantPriceCalculatorInterface $priceCalculator,
ChannelContextInterface $channelContext
ProductVariantPricesCalculatorInterface $priceCalculator,
ChannelContextInterface $channelContext,
AvailabilityCheckerInterface $availabilityChecker
) {
$this->objectNormalizer = $objectNormalizer;
$this->priceCalculator = $priceCalculator;
$this->channelContext = $channelContext;
$this->availabilityChecker = $availabilityChecker;
}

public function normalize($object, $format = null, array $context = [])
{
Assert::isInstanceOf($object, ProductVariantInterface::class);

$data = $this->objectNormalizer->normalize($object, $format, $context);

try {
$data['price'] = $this->priceCalculator->calculate($object, ['channel' => $this->channelContext->getChannel()]);
$data['inStock'] = $this->availabilityChecker->isStockAvailable($object);
arti0090 marked this conversation as resolved.
Show resolved Hide resolved
} catch (ChannelNotFoundException $exception) {
unset($data['price']);
}
Expand All @@ -50,10 +57,10 @@ public function normalize($object, $format = null, array $context = [])

public function supportsNormalization($data, $format = null, $context = []): bool
{
return $data instanceof ProductVariantInterface && $this->isAdminGetOperation($context);
return $data instanceof ProductVariantInterface && $this->isNotAdminGetOperation($context);
}

private function isAdminGetOperation(array $context): bool
private function isNotAdminGetOperation(array $context): bool
{
return !isset($context['item_operation_name']) || !($context['item_operation_name'] === 'admin_get');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Order\Model\Order;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand All @@ -26,9 +27,10 @@ final class ProductVariantSerializerSpec extends ObjectBehavior
function let(
NormalizerInterface $objectNormalizer,
ProductVariantPricesCalculatorInterface $pricesCalculator,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
AvailabilityCheckerInterface $availabilityChecker
): void {
$this->beConstructedWith($objectNormalizer, $pricesCalculator, $channelContext);
$this->beConstructedWith($objectNormalizer, $pricesCalculator, $channelContext, $availabilityChecker);
}

function it_supports_only_product_variant_interface(): void
Expand All @@ -50,15 +52,17 @@ function it_serializes_product_variant_if_item_operation_name_is_different_that_
NormalizerInterface $objectNormalizer,
ProductVariantPricesCalculatorInterface $pricesCalculator,
ChannelInterface $channel,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
AvailabilityCheckerInterface $availabilityChecker
): void {
$variant = new ProductVariant();

$objectNormalizer->normalize($variant, null, [])->willReturn([]);

$channelContext->getChannel()->willReturn($channel);
$pricesCalculator->calculate($variant, ['channel' => $channel])->willReturn(1000);
$availabilityChecker->isStockAvailable($variant)->willReturn(true);

$this->normalize($variant, null, [])->shouldReturn(['price' => 1000]);
$this->normalize($variant, null, [])->shouldReturn(['price' => 1000, 'inStock' => true]);
}
}