Completed
Pull Request — master (#290)
by
unknown
41:51
created

AddProductReviewBySlugHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A handle() 25 25 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ShopApiPlugin\Command\AddProductReviewBySlug;
13
use Sylius\ShopApiPlugin\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);
0 ignored issues
show
Bug introduced by
It seems like $product defined by $this->productRepository...dReview->productSlug()) on line 64 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...
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