Completed
Push — master ( 011918...3a8b9a )
by Paweł
31:19 queued 19:29
created

ProductReviewContext::createProductReview()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 6
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\ProductReviewTransitions;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\Component\Review\Model\ReviewInterface;
23
24
/**
25
 * @author Magdalena Banasiak <[email protected]>
26
 */
27
final class ProductReviewContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $productReviewFactory;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $productReviewRepository;
43
44
    /**
45
     * @var StateMachineFactoryInterface
46
     */
47
    private $stateMachineFactory;
48
49
    /**
50
     * @param SharedStorageInterface $sharedStorage
51
     * @param FactoryInterface $productReviewFactory
52
     * @param RepositoryInterface $productReviewRepository
53
     * @param StateMachineFactoryInterface $stateMachineFactory
54
     */
55
    public function __construct(
56
        SharedStorageInterface $sharedStorage,
57
        FactoryInterface $productReviewFactory,
58
        RepositoryInterface $productReviewRepository,
59
        StateMachineFactoryInterface $stateMachineFactory
60
    ) {
61
        $this->sharedStorage = $sharedStorage;
62
        $this->productReviewFactory = $productReviewFactory;
63
        $this->productReviewRepository = $productReviewRepository;
64
        $this->stateMachineFactory = $stateMachineFactory;
65
    }
66
67
    /**
68
     * @Given /^(this product) has one review$/
69
     */
70
    public function productHasAReview(ProductInterface $product)
71
    {
72
        $review = $this->createProductReview($product, 'Title', 5, 'Comment');
73
74
        $this->productReviewRepository->add($review);
75
    }
76
77
    /**
78
     * @Given /^(this product) has(?:| also) a review titled "([^"]+)" and rated (\d+) added by (customer "[^"]+")(?:|, created (\d+) days ago)$/
79
     */
80
    public function thisProductHasAReviewTitledAndRatedAddedByCustomer(
81
        ProductInterface $product,
82
        $title,
83
        $rating,
84
        CustomerInterface $customer,
85
        $daysSinceCreation = null
86
    ) {
87
        $review = $this->createProductReview($product, $title, $rating, $title, $customer);
88
        if (null !== $daysSinceCreation) {
89
            $review->setCreatedAt(new \DateTime('-'.$daysSinceCreation.' days'));
90
        }
91
92
        $this->productReviewRepository->add($review);
93
    }
94
95
    /**
96
     * @Given /^(this product) has(?:| also) a review titled "([^"]+)" and rated (\d+) added by (customer "[^"]+") which is not accepted yet$/
97
     */
98
    public function thisProductHasAReviewTitledAndRatedAddedByCustomerWhichIsNotAcceptedYet(
99
        ProductInterface $product,
100
        $title,
101
        $rating,
102
        CustomerInterface $customer
103
    ) {
104
        $review = $this->createProductReview($product, $title, $rating, $title, $customer, null);
105
106
        $this->productReviewRepository->add($review);
107
    }
108
109
    /**
110
     * @Given /^(this product)(?:| also) has accepted reviews rated (\d+), (\d+), (\d+), (\d+) and (\d+)$/
111
     * @Given /^(this product)(?:| also) has accepted reviews rated (\d+), (\d+) and (\d+)$/
112
     */
113
    public function thisProductHasAcceptedReviewsRated(ProductInterface $product, ...$rates)
114
    {
115
        $customer = $this->sharedStorage->get('customer');
116
        foreach ($rates as $key => $rate) {
117
            $review = $this->createProductReview($product, 'Title '.$key, $rate, 'Comment '.$key, $customer);
118
            $this->productReviewRepository->add($review);
119
        }
120
    }
121
122
    /**
123
     * @Given /^(this product)(?:| also) has review rated (\d+) which is not accepted yet$/
124
     */
125
    public function itAlsoHasReviewRatedWhichIsNotAcceptedYet(ProductInterface $product, $rate)
126
    {
127
        $customer = $this->sharedStorage->get('customer');
128
        $review = $this->createProductReview($product, 'Title', $rate, 'Comment', $customer, null);
129
        $this->productReviewRepository->add($review);
130
    }
131
132
    /**
133
     * @Given /^(this product) also has review rated (\d+) which is rejected$/
134
     */
135
    public function itAlsoHasReviewRatedWhichIsRejected(ProductInterface $product, $rate)
136
    {
137
        $customer = $this->sharedStorage->get('customer');
138
        $review = $this->createProductReview($product, 'Title', $rate, 'Comment', $customer, ProductReviewTransitions::TRANSITION_REJECT);
139
        $this->productReviewRepository->add($review);
140
    }
141
142
    /**
143
     * @param ProductInterface $product
144
     * @param string $title
145
     * @param int $rating
146
     * @param string $comment
147
     * @param CustomerInterface|null $customer
148
     * @param string $transition
149
     *
150
     * @return ReviewInterface
151
     */
152
    private function createProductReview(
153
        ProductInterface $product,
154
        $title,
155
        $rating,
156
        $comment,
157
        CustomerInterface $customer = null,
158
        $transition = ProductReviewTransitions::TRANSITION_ACCEPT
159
    ) {
160
        /** @var ReviewInterface $review */
161
        $review = $this->productReviewFactory->createNew();
162
        $review->setTitle($title);
163
        $review->setRating($rating);
164
        $review->setComment($comment);
165
        $review->setReviewSubject($product);
166
        $review->setAuthor($customer);
0 ignored issues
show
Bug introduced by
It seems like $customer defined by parameter $customer on line 157 can also be of type object<Sylius\Component\...odel\CustomerInterface>; however, Sylius\Component\Review\...wInterface::setAuthor() does only seem to accept null|object<Sylius\Compo...odel\ReviewerInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
167
168
        $product->addReview($review);
169
170
        if (null !== $transition) {
171
            $stateMachine = $this->stateMachineFactory->get($review, ProductReviewTransitions::GRAPH);
172
            $stateMachine->apply($transition);
173
        }
174
175
        return $review;
176
    }
177
}
178