@@ 16-69 (lines=54) @@ | ||
13 | use Sylius\ShopApiPlugin\Provider\ProductReviewerProviderInterface; |
|
14 | use Webmozart\Assert\Assert; |
|
15 | ||
16 | final class AddProductReviewByCodeHandler |
|
17 | { |
|
18 | /** @var ProductReviewRepositoryInterface */ |
|
19 | private $productReviewRepository; |
|
20 | ||
21 | /** @var ChannelRepositoryInterface */ |
|
22 | private $channelRepository; |
|
23 | ||
24 | /** @var ProductRepositoryInterface */ |
|
25 | private $productRepository; |
|
26 | ||
27 | /** @var ProductReviewerProviderInterface */ |
|
28 | private $productReviewerProvider; |
|
29 | ||
30 | /** @var ReviewFactoryInterface */ |
|
31 | private $reviewFactory; |
|
32 | ||
33 | public function __construct( |
|
34 | ProductReviewRepositoryInterface $productReviewRepository, |
|
35 | ChannelRepositoryInterface $channelRepository, |
|
36 | ProductRepositoryInterface $productRepository, |
|
37 | ProductReviewerProviderInterface $productReviewerProvider, |
|
38 | ReviewFactoryInterface $reviewFactory |
|
39 | ) { |
|
40 | $this->productReviewRepository = $productReviewRepository; |
|
41 | $this->channelRepository = $channelRepository; |
|
42 | $this->productRepository = $productRepository; |
|
43 | $this->productReviewerProvider = $productReviewerProvider; |
|
44 | $this->reviewFactory = $reviewFactory; |
|
45 | } |
|
46 | ||
47 | public function handle(AddProductReviewByCode $addReview): void |
|
48 | { |
|
49 | /** @var ChannelInterface $channel */ |
|
50 | $channel = $this->channelRepository->findOneByCode($addReview->channelCode()); |
|
51 | ||
52 | Assert::notNull($channel, 'Channel not found.'); |
|
53 | ||
54 | $product = $this->productRepository->findOneByCode($addReview->productCode()); |
|
55 | ||
56 | Assert::notNull($product, 'Product not found.'); |
|
57 | Assert::true($product->hasChannel($channel), 'Product is not enabled for given channel.'); |
|
58 | ||
59 | $productReviewer = $this->productReviewerProvider->provide($addReview->email()); |
|
60 | ||
61 | $productReview = $this->reviewFactory->createForSubjectWithReviewer($product, $productReviewer); |
|
62 | ||
63 | $productReview->setComment($addReview->comment()); |
|
64 | $productReview->setRating($addReview->rating()); |
|
65 | $productReview->setTitle($addReview->title()); |
|
66 | ||
67 | $this->productReviewRepository->add($productReview); |
|
68 | } |
|
69 | } |
|
70 |
@@ 16-82 (lines=67) @@ | ||
13 | use Sylius\ShopApiPlugin\Provider\ProductReviewerProviderInterface; |
|
14 | use Webmozart\Assert\Assert; |
|
15 | ||
16 | final class AddProductReviewBySlugHandler |
|
17 | { |
|
18 | /** |
|
19 | * @var ProductReviewRepositoryInterface |
|
20 | */ |
|
21 | private $productReviewRepository; |
|
22 | ||
23 | /** |
|
24 | * @var ChannelRepositoryInterface |
|
25 | */ |
|
26 | private $channelRepository; |
|
27 | ||
28 | /** |
|
29 | * @var ProductRepositoryInterface |
|
30 | */ |
|
31 | private $productRepository; |
|
32 | ||
33 | /** |
|
34 | * @var ProductReviewerProviderInterface |
|
35 | */ |
|
36 | private $productReviewerProvider; |
|
37 | ||
38 | /** |
|
39 | * @var ReviewFactoryInterface |
|
40 | */ |
|
41 | private $reviewFactory; |
|
42 | ||
43 | public function __construct( |
|
44 | ProductReviewRepositoryInterface $productReviewRepository, |
|
45 | ChannelRepositoryInterface $channelRepository, |
|
46 | ProductRepositoryInterface $productRepository, |
|
47 | ProductReviewerProviderInterface $productReviewerProvider, |
|
48 | ReviewFactoryInterface $reviewFactory |
|
49 | ) { |
|
50 | $this->productReviewRepository = $productReviewRepository; |
|
51 | $this->channelRepository = $channelRepository; |
|
52 | $this->productRepository = $productRepository; |
|
53 | $this->productReviewerProvider = $productReviewerProvider; |
|
54 | $this->reviewFactory = $reviewFactory; |
|
55 | } |
|
56 | ||
57 | public function handle(AddProductReviewBySlug $addReview): void |
|
58 | { |
|
59 | /** @var ChannelInterface $channel */ |
|
60 | $channel = $this->channelRepository->findOneByCode($addReview->channelCode()); |
|
61 | ||
62 | Assert::notNull($channel, 'Channel not found.'); |
|
63 | ||
64 | $product = $this->productRepository->findOneByChannelAndSlug( |
|
65 | $channel, |
|
66 | $channel->getDefaultLocale()->getCode(), |
|
67 | $addReview->productSlug() |
|
68 | ); |
|
69 | ||
70 | Assert::notNull($product, 'Product not found.'); |
|
71 | ||
72 | $productReviewer = $this->productReviewerProvider->provide($addReview->email()); |
|
73 | ||
74 | $productReview = $this->reviewFactory->createForSubjectWithReviewer($product, $productReviewer); |
|
75 | ||
76 | $productReview->setComment($addReview->comment()); |
|
77 | $productReview->setRating($addReview->rating()); |
|
78 | $productReview->setTitle($addReview->title()); |
|
79 | ||
80 | $this->productReviewRepository->add($productReview); |
|
81 | } |
|
82 | } |
|
83 |