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\ShopApiPlugin\Command\AddProductReviewByCode; |
13
|
|
|
use Sylius\ShopApiPlugin\Provider\ProductReviewerProviderInterface; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.