|
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\Uploader\ImageUploaderInterface; |
|
23
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
25
|
|
|
|
|
26
|
|
|
final class PageFixtureFactory implements FixtureFactoryInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var FactoryInterface */ |
|
29
|
|
|
private $pageFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** @var FactoryInterface */ |
|
32
|
|
|
private $pageTranslationFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** @var PageRepositoryInterface */ |
|
35
|
|
|
private $pageRepository; |
|
36
|
|
|
|
|
37
|
|
|
/** @var ImageUploaderInterface */ |
|
38
|
|
|
private $imageUploader; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ProductsAssignerInterface */ |
|
41
|
|
|
private $productsAssigner; |
|
42
|
|
|
|
|
43
|
|
|
/** @var SectionsAssignerInterface */ |
|
44
|
|
|
private $sectionsAssigner; |
|
45
|
|
|
|
|
46
|
|
|
/** @var ChannelsAssignerInterface */ |
|
47
|
|
|
private $channelAssigner; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
FactoryInterface $pageFactory, |
|
52
|
|
|
FactoryInterface $pageTranslationFactory, |
|
53
|
|
|
PageRepositoryInterface $pageRepository, |
|
54
|
|
|
ImageUploaderInterface $imageUploader, |
|
55
|
|
|
ProductsAssignerInterface $productsAssigner, |
|
56
|
|
|
SectionsAssignerInterface $sectionsAssigner, |
|
57
|
|
|
ChannelsAssignerInterface $channelAssigner |
|
58
|
|
|
) { |
|
59
|
|
|
$this->pageFactory = $pageFactory; |
|
60
|
|
|
$this->pageTranslationFactory = $pageTranslationFactory; |
|
61
|
|
|
$this->pageRepository = $pageRepository; |
|
62
|
|
|
$this->imageUploader = $imageUploader; |
|
63
|
|
|
$this->productsAssigner = $productsAssigner; |
|
64
|
|
|
$this->sectionsAssigner = $sectionsAssigner; |
|
65
|
|
|
$this->channelAssigner = $channelAssigner; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function load(array $data): void |
|
69
|
|
|
{ |
|
70
|
|
|
foreach ($data as $code => $fields) { |
|
71
|
|
|
if ( |
|
72
|
|
|
true === $fields['remove_existing'] && |
|
73
|
|
|
null !== $page = $this->pageRepository->findOneBy(['code' => $code]) |
|
74
|
|
|
) { |
|
75
|
|
|
$this->pageRepository->remove($page); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (null !== $fields['number']) { |
|
79
|
|
|
for ($i = 0; $i < $fields['number']; ++$i) { |
|
80
|
|
|
$this->createPage(md5(uniqid()), $fields, true); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
$this->createPage($code, $fields); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function createPage(string $code, array $pageData, bool $generateSlug = false): void |
|
89
|
|
|
{ |
|
90
|
|
|
/** @var PageInterface $page */ |
|
91
|
|
|
$page = $this->pageFactory->createNew(); |
|
92
|
|
|
|
|
93
|
|
|
$this->sectionsAssigner->assign($page, $pageData['sections']); |
|
94
|
|
|
$this->productsAssigner->assign($page, $pageData['products']); |
|
95
|
|
|
$this->channelAssigner->assign($page, $pageData['channels']); |
|
96
|
|
|
|
|
97
|
|
|
$page->setCode($code); |
|
98
|
|
|
$page->setEnabled($pageData['enabled']); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($pageData['translations'] as $localeCode => $translation) { |
|
101
|
|
|
/** @var PageTranslationInterface $pageTranslation */ |
|
102
|
|
|
$pageTranslation = $this->pageTranslationFactory->createNew(); |
|
103
|
|
|
$slug = true === $generateSlug ? md5(uniqid()) : $translation['slug']; |
|
104
|
|
|
|
|
105
|
|
|
$pageTranslation->setLocale($localeCode); |
|
106
|
|
|
$pageTranslation->setSlug($slug); |
|
107
|
|
|
$pageTranslation->setName($translation['name']); |
|
108
|
|
|
$pageTranslation->setMetaKeywords($translation['meta_keywords']); |
|
109
|
|
|
$pageTranslation->setMetaDescription($translation['meta_description']); |
|
110
|
|
|
$pageTranslation->setContent($translation['content']); |
|
111
|
|
|
|
|
112
|
|
|
if ($translation['image_path']) { |
|
113
|
|
|
$image = new PageImage(); |
|
114
|
|
|
$path = $translation['image_path']; |
|
115
|
|
|
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg'); |
|
116
|
|
|
|
|
117
|
|
|
$image->setFile($uploadedImage); |
|
118
|
|
|
$pageTranslation->setImage($image); |
|
119
|
|
|
|
|
120
|
|
|
$this->imageUploader->upload($image); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$page->addTranslation($pageTranslation); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->pageRepository->add($page); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|