Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

AddProductReviewBySlugHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\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\SyliusShopApiPlugin\Command\AddProductReviewBySlug;
13
use Sylius\SyliusShopApiPlugin\Provider\ProductReviewerProviderInterface;
14
use Webmozart\Assert\Assert;
15
16 View Code Duplication
final class AddProductReviewBySlugHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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