-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
features/product/viewing_products/viewing_product_via_slug.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@viewing_products | ||
Feature: Viewing a product details using slug | ||
In order to see products detailed information | ||
As a Visitor | ||
I want to be able to view a single product using slug | ||
|
||
Background: | ||
Given the store operates on a single channel in "United States" | ||
And the store has a product "T-shirt banana" | ||
And the description of product "T-shirt banana" is "You must have this beautiful T-shirt" | ||
|
||
@api | ||
Scenario: Viewing a detailed page with product's slug | ||
When I view product "T-shirt banana" using slug | ||
Then I should be redirected to "T-shirt banana" product |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Sylius/Bundle/ApiBundle/Controller/GetProductBySlugAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Controller; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class GetProductBySlugAction | ||
{ | ||
private ChannelContextInterface $channelContext; | ||
private LocaleContextInterface $localeContext; | ||
private ProductRepositoryInterface $productRepository; | ||
private IriConverterInterface $iriConverter; | ||
private RequestStack $requestStack; | ||
|
||
public function __construct(ChannelContextInterface $channelContext, LocaleContextInterface $localeContext, ProductRepositoryInterface $productRepository, IriConverterInterface $iriConverter, RequestStack $requestStack) | ||
{ | ||
$this->channelContext = $channelContext; | ||
$this->localeContext = $localeContext; | ||
$this->productRepository = $productRepository; | ||
$this->iriConverter = $iriConverter; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function __invoke(string $slug): RedirectResponse | ||
{ | ||
$channel = $this->channelContext->getChannel(); | ||
$locale = $this->localeContext->getLocaleCode(); | ||
|
||
$product = $this->productRepository->findOneByChannelAndSlug($channel, $locale, $slug); | ||
|
||
if (null === $product) { | ||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
$iri = $this->iriConverter->getIriFromItem($product); | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
$requestQuery = $request->getQueryString(); | ||
if (null !== $requestQuery) { | ||
$iri .= sprintf('?%s', $requestQuery); | ||
} | ||
|
||
return new RedirectResponse($iri, Response::HTTP_MOVED_PERMANENTLY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Sylius/Bundle/ApiBundle/Swagger/ProductSlugDocumentationNormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Swagger; | ||
|
||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** @experimental */ | ||
final class ProductSlugDocumentationNormalizer implements NormalizerInterface | ||
{ | ||
private const PRODUCT_SLUG_PATH = '/api/v2/shop/products/slug/{slug}'; | ||
|
||
private NormalizerInterface $decoratedNormalizer; | ||
|
||
public function __construct(NormalizerInterface $decoratedNormalizer) | ||
{ | ||
$this->decoratedNormalizer = $decoratedNormalizer; | ||
} | ||
|
||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $this->decoratedNormalizer->supportsNormalization($data, $format); | ||
} | ||
|
||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
$docs = $this->decoratedNormalizer->normalize($object, $format, $context); | ||
|
||
$params = $docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters']; | ||
|
||
foreach ($params as $index => $param) { | ||
if ($param['name'] === 'code') { | ||
unset($docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters'][$index]); | ||
} | ||
} | ||
|
||
return $docs; | ||
} | ||
} |