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\SyliusCmsPlugin\Fixture\Factory; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Entity\SectionInterface; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; |
19
|
|
|
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface; |
20
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
21
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
22
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
23
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Mikołaj Król <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class PageFixtureFactory implements FixtureFactoryInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var FactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $pageFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $pageTranslationFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var PageRepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $pageRepository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ProductRepositoryInterface |
47
|
|
|
*/ |
48
|
|
|
private $productRepository; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var SectionRepositoryInterface |
52
|
|
|
*/ |
53
|
|
|
private $sectionRepository; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ChannelContextInterface |
57
|
|
|
*/ |
58
|
|
|
private $channelContext; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var LocaleContextInterface |
62
|
|
|
*/ |
63
|
|
|
private $localeContext; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param FactoryInterface $pageFactory |
67
|
|
|
* @param FactoryInterface $pageTranslationFactory |
68
|
|
|
* @param PageRepositoryInterface $pageRepository |
69
|
|
|
* @param ProductRepositoryInterface $productRepository |
70
|
|
|
* @param SectionRepositoryInterface $sectionRepository |
71
|
|
|
* @param ChannelContextInterface $channelContext |
72
|
|
|
* @param LocaleContextInterface $localeContext |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
FactoryInterface $pageFactory, |
76
|
|
|
FactoryInterface $pageTranslationFactory, |
77
|
|
|
PageRepositoryInterface $pageRepository, |
78
|
|
|
ProductRepositoryInterface $productRepository, |
79
|
|
|
SectionRepositoryInterface $sectionRepository, |
80
|
|
|
ChannelContextInterface $channelContext, |
81
|
|
|
LocaleContextInterface $localeContext |
82
|
|
|
) |
83
|
|
|
{ |
84
|
|
|
$this->pageFactory = $pageFactory; |
85
|
|
|
$this->pageTranslationFactory = $pageTranslationFactory; |
86
|
|
|
$this->pageRepository = $pageRepository; |
87
|
|
|
$this->productRepository = $productRepository; |
88
|
|
|
$this->sectionRepository = $sectionRepository; |
89
|
|
|
$this->channelContext = $channelContext; |
90
|
|
|
$this->localeContext = $localeContext; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function load(array $data): void |
97
|
|
|
{ |
98
|
|
|
foreach ($data as $code => $fields) { |
99
|
|
|
if ( |
100
|
|
|
true === $fields['remove_existing'] && |
101
|
|
|
null !== $page = $this->pageRepository->findOneBy(['code' => $code]) |
102
|
|
|
) { |
103
|
|
|
$this->pageRepository->remove($page); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (null !== $fields['number']) { |
107
|
|
|
for ($i = 0; $i < $fields['number']; $i++) { |
108
|
|
|
$this->createPage(md5(uniqid()), $fields,true); |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
$this->createPage($code, $fields); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $code |
118
|
|
|
* @param array $pageData |
119
|
|
|
* @param bool $generateSlug |
120
|
|
|
*/ |
121
|
|
|
private function createPage(string $code, array $pageData, bool $generateSlug = false): void |
122
|
|
|
{ |
123
|
|
|
/** @var PageInterface $page */ |
124
|
|
|
$page = $this->pageFactory->createNew(); |
125
|
|
|
$products = $pageData['products']; |
126
|
|
|
|
127
|
|
|
if (null !== $products) { |
128
|
|
|
$this->resolveProducts($page, $products); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->resolveSections($page, $pageData['sections']); |
132
|
|
|
|
133
|
|
|
$page->setCode($code); |
134
|
|
|
$page->setEnabled($pageData['enabled']); |
135
|
|
|
|
136
|
|
|
foreach ($pageData['translations'] as $localeCode => $translation) { |
137
|
|
|
/** @var PageTranslationInterface $pageTranslation */ |
138
|
|
|
$pageTranslation = $this->pageTranslationFactory->createNew(); |
139
|
|
|
$slug = true === $generateSlug ? md5(uniqid()) : $translation['slug']; |
140
|
|
|
|
141
|
|
|
$pageTranslation->setLocale($localeCode); |
142
|
|
|
$pageTranslation->setSlug($slug); |
143
|
|
|
$pageTranslation->setName($translation['name']); |
144
|
|
|
$pageTranslation->setMetaKeywords($translation['meta_keywords']); |
145
|
|
|
$pageTranslation->setMetaDescription($translation['meta_description']); |
146
|
|
|
$pageTranslation->setContent($translation['content']); |
147
|
|
|
|
148
|
|
|
$page->addTranslation($pageTranslation); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->pageRepository->add($page); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $limit |
156
|
|
|
* @param PageInterface $page |
157
|
|
|
*/ |
158
|
|
|
private function resolveProducts(PageInterface $page, int $limit): void |
159
|
|
|
{ |
160
|
|
|
$products = $this->productRepository->findLatestByChannel( |
161
|
|
|
$this->channelContext->getChannel(), |
162
|
|
|
$this->localeContext->getLocaleCode(), |
163
|
|
|
$limit |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
foreach ($products as $product) { |
167
|
|
|
$page->addProduct($product); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param PageInterface $page |
173
|
|
|
* @param array $sections |
174
|
|
|
*/ |
175
|
|
|
private function resolveSections(PageInterface $page, array $sections): void |
176
|
|
|
{ |
177
|
|
|
foreach ($sections as $sectionCode) { |
178
|
|
|
/** @var SectionInterface $section */ |
179
|
|
|
$section = $this->sectionRepository->findOneBy(['code' => $sectionCode]); |
180
|
|
|
|
181
|
|
|
$page->addSection($section); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|