Skip to content

Commit

Permalink
[API][ProductReview] Validate product field
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Jul 3, 2023
1 parent b644746 commit 6f6bf56
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
namespace Sylius\Bundle\ApiBundle\CommandHandler\Catalog;

use Sylius\Bundle\ApiBundle\Command\Catalog\AddProductReview;
use Sylius\Bundle\ApiBundle\Exception\ProductNotFoundException;
use Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Review\Model\ReviewInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

/** @experimental */
Expand All @@ -36,9 +38,13 @@ public function __construct(

public function __invoke(AddProductReview $addProductReview): ReviewInterface
{
/** @var ProductInterface $product */
/** @var ProductInterface|null $product */
$product = $this->productRepository->findOneByCode($addProductReview->productCode);

if ($product === null) {
throw new ProductNotFoundException(Response::HTTP_UNPROCESSABLE_ENTITY);
}

/** @var string|null $email */
$email = $addProductReview->getEmail();

Expand Down
26 changes: 26 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Exception/ProductNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

/** @experimental */
final class ProductNotFoundException extends HttpException
{
public function __construct(int $statusCode = Response::HTTP_NOT_FOUND)
{
parent::__construct($statusCode, 'Product not found.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
</option>
</constraint>
</property>
<property name="productCode">
<constraint name="NotBlank">
<option name="message">sylius.review.product.not_blank</option>
<option name="groups">
<value>sylius</value>
</option>
</constraint>
</property>
<property name="email">
<constraint name="NotBlank">
<option name="message">sylius.review.author.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Catalog\AddProductReview;
use Sylius\Bundle\ApiBundle\Exception\ProductNotFoundException;
use Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ProductInterface;
Expand Down Expand Up @@ -91,4 +92,21 @@ function it_throws_an_exception_if_email_has_not_been_found(
])
;
}

function it_throws_an_exception_if_product_has_not_been_found(ProductRepositoryInterface $productRepository): void
{
$productRepository->findOneByCode('winter_cap')->willReturn(null);

$this
->shouldThrow(ProductNotFoundException::class)
->during('__invoke', [
new AddProductReview(
'Good stuff',
5,
'Really good stuff',
'winter_cap',
),
])
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ sylius:
review:
author:
not_blank: Please enter your email.
product:
not_blank: Please enter a product.
rating:
range: Review rating must be an integer in the range 1-5.
not_in_range: Review rating must be between {{ min }} and {{ max }}.
Expand Down

0 comments on commit 6f6bf56

Please sign in to comment.