Completed
Pull Request — master (#180)
by Łukasz
05:27 queued 02:14
created

AddProductReviewByCodeHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
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
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
    /** @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);
0 ignored issues
show
Bug introduced by
It seems like $product defined by $this->productRepository...dReview->productCode()) on line 54 can be null; however, Sylius\Component\Review\...orSubjectWithReviewer() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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