Completed
Pull Request — master (#146)
by Łukasz
10:35 queued 06:48
created

AddReviewHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 10
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B handle() 0 25 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\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);
0 ignored issues
show
Bug introduced by
It seems like $product defined by $this->productRepository...dReview->productSlug()) on line 65 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...
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