1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by 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\Assigner\ChannelsAssignerInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageImage; |
19
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageInterface; |
20
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface; |
21
|
|
|
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; |
22
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
23
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
24
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
25
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
26
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
27
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
28
|
|
|
|
29
|
|
|
final class PageFixtureFactory implements FixtureFactoryInterface |
30
|
|
|
{ |
31
|
|
|
/** @var FactoryInterface */ |
32
|
|
|
private $pageFactory; |
33
|
|
|
|
34
|
|
|
/** @var FactoryInterface */ |
35
|
|
|
private $pageTranslationFactory; |
36
|
|
|
|
37
|
|
|
/** @var PageRepositoryInterface */ |
38
|
|
|
private $pageRepository; |
39
|
|
|
|
40
|
|
|
/** @var ImageUploaderInterface */ |
41
|
|
|
private $imageUploader; |
42
|
|
|
|
43
|
|
|
/** @var ProductRepositoryInterface */ |
44
|
|
|
private $productRepository; |
45
|
|
|
|
46
|
|
|
/** @var ChannelContextInterface */ |
47
|
|
|
private $channelContext; |
48
|
|
|
|
49
|
|
|
/** @var LocaleContextInterface */ |
50
|
|
|
private $localeContext; |
51
|
|
|
|
52
|
|
|
/** @var ProductsAssignerInterface */ |
53
|
|
|
private $productsAssigner; |
54
|
|
|
|
55
|
|
|
/** @var SectionsAssignerInterface */ |
56
|
|
|
private $sectionsAssigner; |
57
|
|
|
|
58
|
|
|
/** @var ChannelsAssignerInterface */ |
59
|
|
|
private $channelAssigner; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function __construct( |
63
|
|
|
FactoryInterface $pageFactory, |
64
|
|
|
FactoryInterface $pageTranslationFactory, |
65
|
|
|
PageRepositoryInterface $pageRepository, |
66
|
|
|
ImageUploaderInterface $imageUploader, |
67
|
|
|
ProductsAssignerInterface $productsAssigner, |
68
|
|
|
SectionsAssignerInterface $sectionsAssigner, |
69
|
|
|
ChannelsAssignerInterface $channelAssigner, |
70
|
|
|
ProductRepositoryInterface $productRepository, |
71
|
|
|
ChannelContextInterface $channelContext, |
72
|
|
|
LocaleContextInterface $localeContext |
73
|
|
|
) { |
74
|
|
|
$this->pageFactory = $pageFactory; |
75
|
|
|
$this->pageTranslationFactory = $pageTranslationFactory; |
76
|
|
|
$this->pageRepository = $pageRepository; |
77
|
|
|
$this->imageUploader = $imageUploader; |
78
|
|
|
$this->productsAssigner = $productsAssigner; |
79
|
|
|
$this->sectionsAssigner = $sectionsAssigner; |
80
|
|
|
$this->channelAssigner = $channelAssigner; |
81
|
|
|
$this->productRepository = $productRepository; |
82
|
|
|
$this->channelContext = $channelContext; |
83
|
|
|
$this->localeContext = $localeContext; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function load(array $data): void |
87
|
|
|
{ |
88
|
|
|
foreach ($data as $code => $fields) { |
89
|
|
|
if ( |
90
|
|
|
true === $fields['remove_existing'] && |
91
|
|
|
null !== $page = $this->pageRepository->findOneBy(['code' => $code]) |
92
|
|
|
) { |
93
|
|
|
$this->pageRepository->remove($page); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (null !== $fields['number']) { |
97
|
|
|
for ($i = 0; $i < $fields['number']; ++$i) { |
98
|
|
|
$this->createPage(md5(uniqid()), $fields, true); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
$this->createPage($code, $fields); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function createPage(string $code, array $pageData, bool $generateSlug = false): void |
107
|
|
|
{ |
108
|
|
|
/** @var PageInterface $page */ |
109
|
|
|
$page = $this->pageFactory->createNew(); |
110
|
|
|
$products = $pageData['products']; |
111
|
|
|
if (null !== $products) { |
112
|
|
|
$this->resolveProducts($page, $products); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->sectionsAssigner->assign($page, $pageData['sections']); |
116
|
|
|
$this->productsAssigner->assign($page, $pageData['productCodes']); |
117
|
|
|
$this->channelAssigner->assign($page, $pageData['channels']); |
118
|
|
|
|
119
|
|
|
$page->setCode($code); |
120
|
|
|
$page->setEnabled($pageData['enabled']); |
121
|
|
|
|
122
|
|
|
foreach ($pageData['translations'] as $localeCode => $translation) { |
123
|
|
|
/** @var PageTranslationInterface $pageTranslation */ |
124
|
|
|
$pageTranslation = $this->pageTranslationFactory->createNew(); |
125
|
|
|
$slug = true === $generateSlug ? md5(uniqid()) : $translation['slug']; |
126
|
|
|
|
127
|
|
|
$pageTranslation->setLocale($localeCode); |
128
|
|
|
$pageTranslation->setSlug($slug); |
129
|
|
|
$pageTranslation->setName($translation['name']); |
130
|
|
|
$pageTranslation->setMetaKeywords($translation['meta_keywords']); |
131
|
|
|
$pageTranslation->setMetaDescription($translation['meta_description']); |
132
|
|
|
$pageTranslation->setContent($translation['content']); |
133
|
|
|
|
134
|
|
|
if ($translation['image_path']) { |
135
|
|
|
$image = new PageImage(); |
136
|
|
|
$path = $translation['image_path']; |
137
|
|
|
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg'); |
138
|
|
|
|
139
|
|
|
$image->setFile($uploadedImage); |
140
|
|
|
$pageTranslation->setImage($image); |
141
|
|
|
|
142
|
|
|
$this->imageUploader->upload($image); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$page->addTranslation($pageTranslation); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->pageRepository->add($page); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function resolveProducts(PageInterface $page, int $limit): void |
152
|
|
|
{ |
153
|
|
|
$products = $this->productRepository->findLatestByChannel( |
154
|
|
|
$this->channelContext->getChannel(), |
155
|
|
|
$this->localeContext->getLocaleCode(), |
156
|
|
|
$limit |
157
|
|
|
); |
158
|
|
|
foreach ($products as $product) { |
159
|
|
|
$page->addProduct($product); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|