Skip to content

Commit

Permalink
Add inStock serialization to productVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed May 17, 2021
1 parent 0a6af7c commit cf1c17b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
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,20 +18,25 @@ final class ProductVariantSerializer implements ContextAwareNormalizerInterface
/** @var NormalizerInterface */
private $objectNormalizer;

/** @var ProductVariantPriceCalculatorInterface */
/** @var ProductVariantPricesCalculatorInterface */
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 = [])
Expand All @@ -39,8 +45,10 @@ public function normalize($object, $format = null, array $context = [])

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


try {
$data['price'] = $this->priceCalculator->calculate($object, ['channel' => $this->channelContext->getChannel()]);
$data['inStock'] = $this->availabilityChecker->isStockAvailable($object);
} catch (ChannelNotFoundException $exception) {
unset($data['price']);
}
Expand All @@ -50,10 +58,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]);
}
}

0 comments on commit cf1c17b

Please sign in to comment.