1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file was created by the developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\CmsPlugin\Fixture; |
14
|
|
|
|
15
|
|
|
use BitBag\CmsPlugin\Entity\PageInterface; |
16
|
|
|
use BitBag\CmsPlugin\Entity\PageTranslationInterface; |
17
|
|
|
use BitBag\CmsPlugin\Entity\SectionInterface; |
18
|
|
|
use BitBag\CmsPlugin\Repository\PageRepositoryInterface; |
19
|
|
|
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface; |
20
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
21
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface; |
22
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
23
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
24
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
25
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Mikołaj Król <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class PageFixture extends AbstractFixture implements FixtureInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var FactoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $pageFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FactoryInterface |
39
|
|
|
*/ |
40
|
|
|
private $pageTranslationFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var PageRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
private $pageRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ProductRepositoryInterface |
49
|
|
|
*/ |
50
|
|
|
private $productRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var SectionRepositoryInterface |
54
|
|
|
*/ |
55
|
|
|
private $sectionRepository; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param FactoryInterface $pageFactory |
59
|
|
|
* @param FactoryInterface $pageTranslationFactory |
60
|
|
|
* @param PageRepositoryInterface $pageRepository |
61
|
|
|
* @param ProductRepositoryInterface $productRepository |
62
|
|
|
* @param SectionRepositoryInterface $sectionRepository |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
FactoryInterface $pageFactory, |
66
|
|
|
FactoryInterface $pageTranslationFactory, |
67
|
|
|
PageRepositoryInterface $pageRepository, |
68
|
|
|
ProductRepositoryInterface $productRepository, |
69
|
|
|
SectionRepositoryInterface $sectionRepository |
70
|
|
|
) |
71
|
|
|
{ |
72
|
|
|
$this->pageFactory = $pageFactory; |
73
|
|
|
$this->pageTranslationFactory = $pageTranslationFactory; |
74
|
|
|
$this->pageRepository = $pageRepository; |
75
|
|
|
$this->productRepository = $productRepository; |
76
|
|
|
$this->sectionRepository = $sectionRepository; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
|
|
public function load(array $options): void |
83
|
|
|
{ |
84
|
|
|
foreach ($options['custom'] as $code => $fields) { |
85
|
|
|
if ( |
86
|
|
|
true === $fields['remove_existing'] && |
87
|
|
|
null !== $page = $this->pageRepository->findOneBy(['code' => $code]) |
88
|
|
|
) { |
89
|
|
|
$this->pageRepository->remove($page); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @var PageInterface $page */ |
93
|
|
|
$page = $this->pageFactory->createNew(); |
94
|
|
|
$products = $fields['products']; |
95
|
|
|
|
96
|
|
|
if (null !== $products) { |
97
|
|
|
$this->resolveProducts($page, $products); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->resolveSections($page, $fields['sections']); |
101
|
|
|
|
102
|
|
|
$page->setCode($code); |
103
|
|
|
$page->setEnabled($fields['enabled']); |
104
|
|
|
|
105
|
|
|
foreach ($fields['translations'] as $localeCode => $translation) { |
106
|
|
|
/** @var PageTranslationInterface $pageTranslation */ |
107
|
|
|
$pageTranslation = $this->pageTranslationFactory->createNew(); |
108
|
|
|
|
109
|
|
|
$pageTranslation->setLocale($localeCode); |
110
|
|
|
$pageTranslation->setSlug($translation['slug']); |
111
|
|
|
$pageTranslation->setName($translation['name']); |
112
|
|
|
$pageTranslation->setMetaKeywords($translation['meta_keywords']); |
113
|
|
|
$pageTranslation->setMetaDescription($translation['meta_description']); |
114
|
|
|
$pageTranslation->setContent($translation['content']); |
115
|
|
|
|
116
|
|
|
$page->addTranslation($pageTranslation); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->pageRepository->add($page); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function getName(): string |
127
|
|
|
{ |
128
|
|
|
return 'bitbag_cms_page'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void |
135
|
|
|
{ |
136
|
|
|
$optionsNode |
137
|
|
|
->children() |
138
|
|
|
->arrayNode('custom') |
139
|
|
|
->prototype('array') |
140
|
|
|
->children() |
141
|
|
|
->booleanNode('remove_existing')->defaultTrue()->end() |
142
|
|
|
->booleanNode('enabled')->defaultTrue()->end() |
143
|
|
|
->integerNode('products')->defaultNull()->end() |
144
|
|
|
->arrayNode('sections') |
145
|
|
|
->prototype('scalar')->end() |
146
|
|
|
->end() |
147
|
|
|
->arrayNode('translations') |
148
|
|
|
->prototype('array') |
149
|
|
|
->children() |
150
|
|
|
->scalarNode('slug')->defaultNull()->end() |
151
|
|
|
->scalarNode('name')->defaultNull()->end() |
152
|
|
|
->scalarNode('meta_keywords')->defaultNull()->end() |
153
|
|
|
->scalarNode('meta_description')->defaultNull()->end() |
154
|
|
|
->scalarNode('content')->defaultNull()->end() |
155
|
|
|
->end() |
156
|
|
|
->end() |
157
|
|
|
->end() |
158
|
|
|
->end() |
159
|
|
|
->end() |
160
|
|
|
->end() |
161
|
|
|
->end() |
162
|
|
|
; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param int $limit |
167
|
|
|
* @param PageInterface $page |
168
|
|
|
*/ |
169
|
|
|
private function resolveProducts(PageInterface $page, int $limit): void |
170
|
|
|
{ |
171
|
|
|
$products = $this->productRepository->findBy([], null, $limit); |
172
|
|
|
|
173
|
|
|
foreach ($products as $product) { |
174
|
|
|
$page->addProduct($product); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param PageInterface $page |
180
|
|
|
* @param array $sections |
181
|
|
|
*/ |
182
|
|
|
private function resolveSections(PageInterface $page, array $sections): void |
183
|
|
|
{ |
184
|
|
|
foreach ($sections as $sectionCode) { |
185
|
|
|
/** @var SectionInterface $section */ |
186
|
|
|
$section = $this->sectionRepository->findOneBy(['code' => $sectionCode]); |
187
|
|
|
|
188
|
|
|
$page->addSection($section); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|