1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lakion\CmsPlugin\Fixture\Factory; |
4
|
|
|
|
5
|
|
|
use Lakion\CmsPlugin\Document\ProductBlock; |
6
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; |
7
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
8
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
9
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
10
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
11
|
|
|
use Symfony\Component\OptionsResolver\Options; |
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
13
|
|
|
|
14
|
|
|
final class ProductBlockExampleFactory implements ExampleFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var FactoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Faker\Generator |
23
|
|
|
*/ |
24
|
|
|
private $faker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var OptionsResolver |
28
|
|
|
*/ |
29
|
|
|
private $optionsResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $productRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param FactoryInterface $factory |
38
|
|
|
* @param RepositoryInterface $productRepository |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FactoryInterface $factory, RepositoryInterface $productRepository) |
41
|
|
|
{ |
42
|
|
|
$this->factory = $factory; |
43
|
|
|
$this->productRepository = $productRepository; |
44
|
|
|
|
45
|
|
|
$this->faker = \Faker\Factory::create(); |
46
|
|
|
$this->optionsResolver = |
47
|
|
|
(new OptionsResolver()) |
48
|
|
|
->setDefault('name', function (Options $options) { |
49
|
|
|
return StringInflector::nameToCode($options['title']); |
50
|
|
|
}) |
51
|
|
|
->setAllowedTypes('name', 'string') |
52
|
|
|
|
53
|
|
|
->setDefault('title', function (Options $options) { |
|
|
|
|
54
|
|
|
return $this->faker->words(3, true); |
55
|
|
|
}) |
56
|
|
|
->setAllowedTypes('title', 'string') |
57
|
|
|
|
58
|
|
|
->setDefault('body', function (Options $options) { |
|
|
|
|
59
|
|
|
return $this->faker->sentence(); |
60
|
|
|
}) |
61
|
|
|
->setAllowedTypes('body', 'string') |
62
|
|
|
|
63
|
|
|
->setDefault('link', function (Options $options) { |
|
|
|
|
64
|
|
|
return $this->faker->url; |
65
|
|
|
}) |
66
|
|
|
->setAllowedTypes('link', 'string') |
67
|
|
|
|
68
|
|
|
->setDefault('product', LazyOption::randomOne($this->productRepository)) |
69
|
|
|
->setNormalizer('product', LazyOption::findOneBy($this->productRepository, 'code')) |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function create(array $options = []) |
77
|
|
|
{ |
78
|
|
|
$options = $this->optionsResolver->resolve($options); |
79
|
|
|
|
80
|
|
|
/** @var ProductBlock $stringBlock */ |
81
|
|
|
$stringBlock = $this->factory->createNew(); |
82
|
|
|
$stringBlock->setName($options['name']); |
83
|
|
|
$stringBlock->setTitle($options['name']); |
84
|
|
|
$stringBlock->setBody($options['body']); |
85
|
|
|
$stringBlock->setLinkUrl($options['link']); |
86
|
|
|
$stringBlock->setProduct($options['product']); |
87
|
|
|
|
88
|
|
|
return $stringBlock; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.