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\Bundle\CoreBundle\Fixture\Factory; |
13
|
|
|
|
14
|
|
|
use SM\Factory\FactoryInterface; |
15
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
16
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
17
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
18
|
|
|
use Sylius\Component\Resource\StateMachine\StateMachineInterface; |
19
|
|
|
use Sylius\Component\Review\Factory\ReviewFactoryInterface; |
20
|
|
|
use Sylius\Component\Review\Model\ReviewInterface; |
21
|
|
|
use Symfony\Component\OptionsResolver\Options; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Mateusz Zalewski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ProductReviewExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ReviewFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $productReviewFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ProductRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $productRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var CustomerRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $customerRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FactoryInterface |
46
|
|
|
*/ |
47
|
|
|
private $stateMachineFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Faker\Generator |
51
|
|
|
*/ |
52
|
|
|
private $faker; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var OptionsResolver |
56
|
|
|
*/ |
57
|
|
|
private $optionsResolver; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ReviewFactoryInterface $productReviewFactory |
61
|
|
|
* @param ProductRepositoryInterface $productRepository |
62
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
63
|
|
|
* @param FactoryInterface $stateMachineFactory |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
ReviewFactoryInterface $productReviewFactory, |
67
|
|
|
ProductRepositoryInterface $productRepository, |
68
|
|
|
CustomerRepositoryInterface $customerRepository, |
69
|
|
|
FactoryInterface $stateMachineFactory |
70
|
|
|
) { |
71
|
|
|
$this->productReviewFactory = $productReviewFactory; |
72
|
|
|
$this->productRepository = $productRepository; |
73
|
|
|
$this->customerRepository = $customerRepository; |
74
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
75
|
|
|
|
76
|
|
|
$this->faker = \Faker\Factory::create(); |
77
|
|
|
$this->optionsResolver = new OptionsResolver(); |
78
|
|
|
|
79
|
|
|
$this->configureOptions($this->optionsResolver); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function create(array $options = []) |
86
|
|
|
{ |
87
|
|
|
$options = $this->optionsResolver->resolve($options); |
88
|
|
|
|
89
|
|
|
/** @var ReviewInterface $productReview */ |
90
|
|
|
$productReview = $this->productReviewFactory->createForSubjectWithReviewer( |
91
|
|
|
$options['product'], |
92
|
|
|
$options['author'] |
93
|
|
|
); |
94
|
|
|
$productReview->setTitle($options['title']); |
95
|
|
|
$productReview->setComment($options['comment']); |
96
|
|
|
$productReview->setRating($options['rating']); |
97
|
|
|
$options['product']->addReview($productReview); |
98
|
|
|
|
99
|
|
|
$this->applyReviewTransition($productReview, $options['status'] ? $options['status'] : $this->getRandomStatus()); |
100
|
|
|
|
101
|
|
|
return $productReview; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
108
|
|
|
{ |
109
|
|
|
$resolver |
110
|
|
|
->setDefault('title', function (Options $options) { |
|
|
|
|
111
|
|
|
return $this->faker->words(3, true); |
112
|
|
|
}) |
113
|
|
|
->setDefault('rating', function (Options $options) { |
|
|
|
|
114
|
|
|
return $this->faker->numberBetween(1, 5); |
115
|
|
|
}) |
116
|
|
|
->setDefault('comment', function (Options $options) { |
|
|
|
|
117
|
|
|
return $this->faker->sentences(3, true); |
118
|
|
|
}) |
119
|
|
|
->setDefault('author', LazyOption::randomOne($this->customerRepository)) |
120
|
|
|
->setNormalizer('author', LazyOption::findOneBy($this->customerRepository, 'email')) |
121
|
|
|
->setDefault('product', LazyOption::randomOne($this->productRepository)) |
122
|
|
|
->setNormalizer('product', LazyOption::findOneBy($this->productRepository, 'code')) |
123
|
|
|
->setDefault('status', null) |
124
|
|
|
; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
private function getRandomStatus() |
131
|
|
|
{ |
132
|
|
|
$statuses = [ReviewInterface::STATUS_NEW, ReviewInterface::STATUS_ACCEPTED, ReviewInterface::STATUS_REJECTED]; |
133
|
|
|
|
134
|
|
|
return $statuses[(rand(0, 2))]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param ReviewInterface $productReview |
139
|
|
|
* @param string $targetState |
140
|
|
|
*/ |
141
|
|
|
private function applyReviewTransition(ReviewInterface $productReview, $targetState) |
142
|
|
|
{ |
143
|
|
|
/** @var StateMachineInterface $stateMachine */ |
144
|
|
|
$stateMachine = $this->stateMachineFactory->get($productReview, 'sylius_product_review'); |
145
|
|
|
$transition = $stateMachine->getTransitionToState($targetState); |
146
|
|
|
|
147
|
|
|
if (null !== $transition) { |
148
|
|
|
$stateMachine->apply($transition); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.