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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\CoreBundle\Fixture\Factory; |
15
|
|
|
|
16
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
17
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
18
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
19
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
21
|
|
|
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface; |
22
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
23
|
|
|
use Symfony\Component\OptionsResolver\Options; |
24
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
25
|
|
|
|
26
|
|
|
class TaxonExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
27
|
|
|
{ |
28
|
|
|
/** @var FactoryInterface */ |
29
|
|
|
private $taxonFactory; |
30
|
|
|
|
31
|
|
|
/** @var TaxonRepositoryInterface */ |
32
|
|
|
private $taxonRepository; |
33
|
|
|
|
34
|
|
|
/** @var RepositoryInterface */ |
35
|
|
|
private $localeRepository; |
36
|
|
|
|
37
|
|
|
/** @var \Faker\Generator */ |
38
|
|
|
private $faker; |
39
|
|
|
|
40
|
|
|
/** @var TaxonSlugGeneratorInterface */ |
41
|
|
|
private $taxonSlugGenerator; |
42
|
|
|
|
43
|
|
|
/** @var OptionsResolver */ |
44
|
|
|
private $optionsResolver; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
FactoryInterface $taxonFactory, |
48
|
|
|
TaxonRepositoryInterface $taxonRepository, |
49
|
|
|
RepositoryInterface $localeRepository, |
50
|
|
|
TaxonSlugGeneratorInterface $taxonSlugGenerator |
51
|
|
|
) { |
52
|
|
|
$this->taxonFactory = $taxonFactory; |
53
|
|
|
$this->taxonRepository = $taxonRepository; |
54
|
|
|
$this->localeRepository = $localeRepository; |
55
|
|
|
$this->taxonSlugGenerator = $taxonSlugGenerator; |
56
|
|
|
|
57
|
|
|
$this->faker = \Faker\Factory::create(); |
58
|
|
|
$this->optionsResolver = new OptionsResolver(); |
59
|
|
|
|
60
|
|
|
$this->configureOptions($this->optionsResolver); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function create(array $options = []): TaxonInterface |
67
|
|
|
{ |
68
|
|
|
return $this->createTaxon($options); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function createTaxon(array $options = [], ?TaxonInterface $parentTaxon = null): ?TaxonInterface |
72
|
|
|
{ |
73
|
|
|
$options = $this->optionsResolver->resolve($options); |
74
|
|
|
|
75
|
|
|
/** @var TaxonInterface $taxon */ |
76
|
|
|
$taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]); |
77
|
|
|
|
78
|
|
|
if (null === $taxon) { |
79
|
|
|
/** @var TaxonInterface $taxon */ |
80
|
|
|
$taxon = $this->taxonFactory->createNew(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$taxon->setCode($options['code']); |
84
|
|
|
|
85
|
|
|
if (null !== $parentTaxon) { |
86
|
|
|
$taxon->setParent($parentTaxon); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// add translation for each defined locales |
90
|
|
|
foreach ($this->getLocales() as $localeCode) { |
91
|
|
|
$this->createTranslation($taxon, $localeCode, $options); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// create or replace with custom translations |
95
|
|
|
foreach ($options['translations'] as $localeCode => $translationOptions) { |
96
|
|
|
$this->createTranslation($taxon, $localeCode, $translationOptions); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($options['children'] as $childOptions) { |
100
|
|
|
$this->createTaxon($childOptions, $taxon); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $taxon; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function createTranslation(TaxonInterface $taxon, string $localeCode, array $options = []): void |
107
|
|
|
{ |
108
|
|
|
$options = $this->optionsResolver->resolve($options); |
109
|
|
|
|
110
|
|
|
$taxon->setCurrentLocale($localeCode); |
111
|
|
|
$taxon->setFallbackLocale($localeCode); |
112
|
|
|
|
113
|
|
|
$taxon->setName($options['name']); |
114
|
|
|
$taxon->setDescription($options['description']); |
115
|
|
|
$taxon->setSlug($options['slug'] ?: $this->taxonSlugGenerator->generate($taxon, $localeCode)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
122
|
|
|
{ |
123
|
|
|
$resolver |
124
|
|
|
->setDefault('name', function (Options $options): string { |
|
|
|
|
125
|
|
|
return $this->faker->words(3, true); |
126
|
|
|
}) |
127
|
|
|
->setDefault('code', function (Options $options): string { |
128
|
|
|
return StringInflector::nameToCode($options['name']); |
129
|
|
|
}) |
130
|
|
|
->setDefault('slug', null) |
131
|
|
|
->setDefault('description', function (Options $options): string { |
|
|
|
|
132
|
|
|
return $this->faker->paragraph; |
133
|
|
|
}) |
134
|
|
|
->setDefault('translations', []) |
135
|
|
|
->setAllowedTypes('translations', ['array']) |
136
|
|
|
->setDefault('children', []) |
137
|
|
|
->setAllowedTypes('children', ['array']) |
138
|
|
|
; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function getLocales(): iterable |
142
|
|
|
{ |
143
|
|
|
/** @var LocaleInterface[] $locales */ |
144
|
|
|
$locales = $this->localeRepository->findAll(); |
145
|
|
|
foreach ($locales as $locale) { |
146
|
|
|
yield $locale->getCode(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.