1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusBannerPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Faker\Factory; |
8
|
|
|
use Generator; |
9
|
|
|
use Odiseo\SyliusBannerPlugin\Entity\BannerInterface; |
10
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
11
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
12
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
13
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
14
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
16
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
18
|
|
|
use Symfony\Component\OptionsResolver\Options; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
|
21
|
|
|
final class BannerExampleFactory extends AbstractExampleFactory |
22
|
|
|
{ |
23
|
|
|
/** @var FactoryInterface */ |
24
|
|
|
private $bannerFactory; |
25
|
|
|
|
26
|
|
|
/** @var ChannelRepositoryInterface */ |
27
|
|
|
private $channelRepository; |
28
|
|
|
|
29
|
|
|
/** @var RepositoryInterface */ |
30
|
|
|
private $localeRepository; |
31
|
|
|
|
32
|
|
|
/** @var \Faker\Generator */ |
33
|
|
|
private $faker; |
34
|
|
|
|
35
|
|
|
/** @var FileLocatorInterface|null */ |
36
|
|
|
private $fileLocator; |
37
|
|
|
|
38
|
|
|
/** @var OptionsResolver */ |
39
|
|
|
private $optionsResolver; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
FactoryInterface $bannerFactory, |
43
|
|
|
ChannelRepositoryInterface $channelRepository, |
44
|
|
|
RepositoryInterface $localeRepository, |
45
|
|
|
?FileLocatorInterface $fileLocator = null |
46
|
|
|
) { |
47
|
|
|
$this->bannerFactory = $bannerFactory; |
48
|
|
|
$this->channelRepository = $channelRepository; |
49
|
|
|
$this->localeRepository = $localeRepository; |
50
|
|
|
$this->fileLocator = $fileLocator; |
51
|
|
|
|
52
|
|
|
$this->faker = Factory::create(); |
53
|
|
|
$this->optionsResolver = new OptionsResolver(); |
54
|
|
|
|
55
|
|
|
$this->configureOptions($this->optionsResolver); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
62
|
|
|
{ |
63
|
|
|
$resolver |
64
|
|
|
->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3)) |
65
|
|
|
->setAllowedTypes('channels', 'array') |
66
|
|
|
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) |
67
|
|
|
|
68
|
|
|
->setDefault('image', function (Options $options): string { |
|
|
|
|
69
|
|
|
return __DIR__.'/../../Resources/fixtures/banner/images/0'.rand(1, 4).'.png'; |
70
|
|
|
}) |
71
|
|
|
->setAllowedTypes('image', ['string']) |
72
|
|
|
|
73
|
|
|
->setDefault('mobile_image', function (Options $options): string { |
|
|
|
|
74
|
|
|
return __DIR__.'/../../Resources/fixtures/banner/mobile-images/0'.rand(1, 4).'.png'; |
75
|
|
|
}) |
76
|
|
|
->setAllowedTypes('mobile_image', ['string']) |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function create(array $options = []): BannerInterface |
84
|
|
|
{ |
85
|
|
|
$options = $this->optionsResolver->resolve($options); |
86
|
|
|
|
87
|
|
|
/** @var BannerInterface $banner */ |
88
|
|
|
$banner = $this->bannerFactory->createNew(); |
89
|
|
|
$banner->setCode($this->faker->slug); |
90
|
|
|
|
91
|
|
|
foreach ($options['channels'] as $channel) { |
92
|
|
|
$banner->addChannel($channel); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @var string $localeCode */ |
96
|
|
|
foreach ($this->getLocales() as $localeCode) { |
97
|
|
|
$banner->setCurrentLocale($localeCode); |
98
|
|
|
$banner->setFallbackLocale($localeCode); |
99
|
|
|
|
100
|
|
|
$banner->setUrl($this->faker->url); |
101
|
|
|
|
102
|
|
|
$banner->setImageFile($this->createImage($options['image'])); |
103
|
|
|
$banner->setMobileImageFile($this->createImage($options['mobile_image'])); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $banner; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $imagePath |
111
|
|
|
* @return UploadedFile |
112
|
|
|
*/ |
113
|
|
|
private function createImage(string $imagePath): UploadedFile |
114
|
|
|
{ |
115
|
|
|
$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath); |
116
|
|
|
|
117
|
|
|
return new UploadedFile($imagePath, basename($imagePath)); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Generator |
122
|
|
|
*/ |
123
|
|
|
private function getLocales(): Generator |
124
|
|
|
{ |
125
|
|
|
/** @var LocaleInterface[] $locales */ |
126
|
|
|
$locales = $this->localeRepository->findAll(); |
127
|
|
|
foreach ($locales as $locale) { |
128
|
|
|
yield $locale->getCode(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.