-
-
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.
feature #12472 [Api] Visitor and Customer add ProductReview (Tomanhez)
This PR was merged into the 1.10-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | License | MIT <!-- - Bug fixes must be submitted against the 1.7 or 1.8 branch (the lowest possible) - Features and deprecations must be submitted against the master branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- f11bfb5 Add specs and behat scenarios bc19b70 Add behat implementation and configuration 2f60dda Add resource configuration and serialization 1900618 Add AddProductReview and AddProductReviewHandler 0cd3da0 AddProductReviewInputDataTransformer d95297d Add next iteration
- Loading branch information
Showing
16 changed files
with
570 additions
and
25 deletions.
There are no files selected for viewing
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
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
26 changes: 26 additions & 0 deletions
26
src/Sylius/Behat/Resources/config/suites/api/product/adding_product_review.yml
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,26 @@ | ||
# This file is part of the Sylius package. | ||
# (c) Paweł Jędrzejewski | ||
|
||
default: | ||
suites: | ||
api_adding_product_review: | ||
contexts: | ||
- sylius.behat.context.hook.doctrine_orm | ||
|
||
- sylius.behat.context.transform.channel | ||
- sylius.behat.context.transform.customer | ||
- sylius.behat.context.transform.lexical | ||
- sylius.behat.context.transform.product | ||
- sylius.behat.context.transform.shared_storage | ||
|
||
- sylius.behat.context.setup.channel | ||
- sylius.behat.context.setup.customer | ||
- sylius.behat.context.setup.product | ||
- sylius.behat.context.setup.product_review | ||
- sylius.behat.context.setup.shop_api_security | ||
|
||
- sylius.behat.context.api.shop.product | ||
- sylius.behat.context.api.shop.product_review | ||
|
||
filters: | ||
tags: "@adding_product_review && @api" |
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,62 @@ | ||
<?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\Command; | ||
|
||
/** @experimental */ | ||
class AddProductReview implements CommandAwareDataTransformerInterface | ||
{ | ||
/** | ||
* @var string | ||
* @psalm-immutable | ||
*/ | ||
public $title; | ||
|
||
/** | ||
* @var int | ||
* @psalm-immutable | ||
*/ | ||
public $rating; | ||
|
||
/** | ||
* @var string | ||
* @psalm-immutable | ||
*/ | ||
public $comment; | ||
|
||
/** | ||
* @var string | ||
* @psalm-immutable | ||
*/ | ||
public $productCode; | ||
|
||
/** | ||
* @var string|null | ||
* @psalm-immutable | ||
*/ | ||
public $email; | ||
|
||
public function __construct( | ||
string $title, | ||
int $rating, | ||
string $comment, | ||
string $productCode, | ||
?string $email = null | ||
) { | ||
$this->title = $title; | ||
$this->rating = $rating; | ||
$this->comment = $comment; | ||
$this->productCode = $productCode; | ||
$this->email = $email; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Sylius/Bundle/ApiBundle/CommandHandler/AddProductReviewHandler.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,85 @@ | ||
<?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\CommandHandler; | ||
|
||
use Sylius\Bundle\AdminApiBundle\Model\UserInterface; | ||
use Sylius\Bundle\ApiBundle\Command\AddProductReview; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface; | ||
use Sylius\Component\Core\Model\CustomerInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
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\Messenger\Handler\MessageHandlerInterface; | ||
|
||
/** @experimental */ | ||
final class AddProductReviewHandler implements MessageHandlerInterface | ||
{ | ||
/** @var FactoryInterface */ | ||
private $productReviewFactory; | ||
|
||
/** @var RepositoryInterface */ | ||
private $productReviewRepository; | ||
|
||
/** @var ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var CustomerProviderInterface */ | ||
private $customerProvider; | ||
|
||
public function __construct( | ||
FactoryInterface $productReviewFactory, | ||
RepositoryInterface $productReviewRepository, | ||
ProductRepositoryInterface $productRepository, | ||
CustomerProviderInterface $customerProvider | ||
) { | ||
$this->productReviewFactory = $productReviewFactory; | ||
$this->productReviewRepository = $productReviewRepository; | ||
$this->productRepository = $productRepository; | ||
$this->customerProvider = $customerProvider; | ||
} | ||
|
||
public function __invoke(AddProductReview $addProductReview): ReviewInterface | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $this->productRepository->findOneByCode($addProductReview->productCode); | ||
|
||
/** @var string|null $email */ | ||
$email = $addProductReview->email; | ||
|
||
if ($email === null) { | ||
throw new \InvalidArgumentException('Visitor should provide an email'); | ||
} | ||
|
||
/** @var CustomerInterface $customer */ | ||
$customer = $this->customerProvider->provide($email); | ||
|
||
/** @var ReviewInterface $review */ | ||
$review = $this->productReviewFactory->createNew(); | ||
$review->setTitle($addProductReview->title); | ||
$review->setRating($addProductReview->rating); | ||
$review->setComment($addProductReview->comment); | ||
$review->setReviewSubject($product); | ||
$review->setAuthor($customer); | ||
|
||
$product->addReview($review); | ||
|
||
$this->productReviewRepository->add($review); | ||
|
||
return $review; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ius/Bundle/ApiBundle/DataTransformer/LoggedInShopUserEmailAwareCommandDataTransformer.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,60 @@ | ||
<?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\DataTransformer; | ||
|
||
use Sylius\Bundle\AdminApiBundle\Model\UserInterface; | ||
use Sylius\Bundle\ApiBundle\Command\AddProductReview; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Component\Core\Model\CustomerInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
|
||
/** @experimental */ | ||
final class LoggedInShopUserEmailAwareCommandDataTransformer implements CommandDataTransformerInterface | ||
{ | ||
/** @var UserContextInterface */ | ||
private $userContext; | ||
|
||
public function __construct(UserContextInterface $userContext) | ||
{ | ||
$this->userContext = $userContext; | ||
} | ||
|
||
public function transform($object, string $to, array $context = []) | ||
{ | ||
/** @var CustomerInterface|null $customer */ | ||
$customer = $this->getCustomer(); | ||
|
||
if ($customer !== null) { | ||
$object->email = $customer->getEmail(); | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
public function supportsTransformation($object): bool | ||
{ | ||
return $object instanceof AddProductReview; | ||
} | ||
|
||
private function getCustomer(): ?CustomerInterface | ||
{ | ||
/** @var UserInterface|null $user */ | ||
$user = $this->userContext->getUser(); | ||
if ($user !== null && $user instanceof ShopUserInterface) { | ||
return $user->getCustomer(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.