1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
9
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
10
|
|
|
use Sylius\Component\Core\Repository\ProductReviewRepositoryInterface; |
11
|
|
|
use Sylius\Component\Review\Factory\ReviewFactoryInterface; |
12
|
|
|
use Sylius\Component\Review\Model\ReviewInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Command\AddReview; |
14
|
|
|
use Sylius\ShopApiPlugin\Provider\ProductReviewerProviderInterface; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
final class AddReviewHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ProductReviewRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $productReviewRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ChannelRepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $channelRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ProductRepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $productRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ProductReviewerProviderInterface |
36
|
|
|
*/ |
37
|
|
|
private $productReviewerProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ReviewFactoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $reviewFactory; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
ProductReviewRepositoryInterface $productReviewRepository, |
46
|
|
|
ChannelRepositoryInterface $channelRepository, |
47
|
|
|
ProductRepositoryInterface $productRepository, |
48
|
|
|
ProductReviewerProviderInterface $productReviewerProvider, |
49
|
|
|
ReviewFactoryInterface $reviewFactory |
50
|
|
|
) { |
51
|
|
|
$this->productReviewRepository = $productReviewRepository; |
52
|
|
|
$this->channelRepository = $channelRepository; |
53
|
|
|
$this->productRepository = $productRepository; |
54
|
|
|
$this->productReviewerProvider = $productReviewerProvider; |
55
|
|
|
$this->reviewFactory = $reviewFactory; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function handle(AddReview $addReview) |
59
|
|
|
{ |
60
|
|
|
/** @var ChannelInterface $channel */ |
61
|
|
|
$channel = $this->channelRepository->findOneByCode($addReview->channelCode()); |
62
|
|
|
|
63
|
|
|
Assert::notNull($channel, 'Channel not found.'); |
64
|
|
|
|
65
|
|
|
$product = $this->productRepository->findOneByChannelAndSlug( |
66
|
|
|
$channel, |
67
|
|
|
$channel->getDefaultLocale()->getCode(), |
68
|
|
|
$addReview->productSlug() |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
Assert::notNull($product, 'Product not found.'); |
72
|
|
|
|
73
|
|
|
$productReviewer = $this->productReviewerProvider->provide($addReview->email()); |
74
|
|
|
|
75
|
|
|
$productReview = $this->reviewFactory->createForSubjectWithReviewer($product, $productReviewer); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$productReview->setComment($addReview->comment()); |
78
|
|
|
$productReview->setRating($addReview->rating()); |
79
|
|
|
$productReview->setTitle($addReview->title()); |
80
|
|
|
|
81
|
|
|
$this->productReviewRepository->add($productReview); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: