1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusVendorPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Faker\Factory; |
8
|
|
|
use Generator; |
9
|
|
|
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface; |
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 VendorExampleFactory extends AbstractExampleFactory |
22
|
|
|
{ |
23
|
|
|
/** @var FactoryInterface */ |
24
|
|
|
private $vendorFactory; |
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 $vendorFactory, |
43
|
|
|
ChannelRepositoryInterface $channelRepository, |
44
|
|
|
RepositoryInterface $localeRepository, |
45
|
|
|
?FileLocatorInterface $fileLocator = null |
46
|
|
|
) { |
47
|
|
|
$this->vendorFactory = $vendorFactory; |
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('logo', function (Options $options): string { |
|
|
|
|
69
|
|
|
return __DIR__.'/../../Resources/fixtures/vendor/0'.rand(1, 4).'.png'; |
70
|
|
|
}) |
71
|
|
|
->setAllowedTypes('logo', ['string']) |
72
|
|
|
; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function create(array $options = []): VendorInterface |
79
|
|
|
{ |
80
|
|
|
$options = $this->optionsResolver->resolve($options); |
81
|
|
|
|
82
|
|
|
/** @var VendorInterface $vendor */ |
83
|
|
|
$vendor = $this->vendorFactory->createNew(); |
84
|
|
|
$vendor->setName($this->faker->company); |
85
|
|
|
$vendor->setSlug($this->faker->slug); |
86
|
|
|
$vendor->setEmail($this->faker->companyEmail); |
87
|
|
|
|
88
|
|
|
foreach ($options['channels'] as $channel) { |
89
|
|
|
$vendor->addChannel($channel); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @var string $localeCode */ |
93
|
|
|
foreach ($this->getLocales() as $localeCode) { |
94
|
|
|
$vendor->setCurrentLocale($localeCode); |
95
|
|
|
$vendor->setFallbackLocale($localeCode); |
96
|
|
|
|
97
|
|
|
$vendor->setDescription($this->faker->text); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$vendor->setLogoFile($this->createImage($options['logo'])); |
101
|
|
|
|
102
|
|
|
return $vendor; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $imagePath |
107
|
|
|
* @return UploadedFile |
108
|
|
|
*/ |
109
|
|
|
private function createImage(string $imagePath): UploadedFile |
110
|
|
|
{ |
111
|
|
|
$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath); |
112
|
|
|
if(is_array($imagePath) && count($imagePath) > 0) |
113
|
|
|
$imagePath = $imagePath[0]; |
114
|
|
|
|
115
|
|
|
return new UploadedFile($imagePath, basename($imagePath)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Generator |
120
|
|
|
*/ |
121
|
|
|
private function getLocales(): Generator |
122
|
|
|
{ |
123
|
|
|
/** @var LocaleInterface[] $locales */ |
124
|
|
|
$locales = $this->localeRepository->findAll(); |
125
|
|
|
foreach ($locales as $locale) { |
126
|
|
|
yield $locale->getCode(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.