|
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\BlockInterface; |
|
19
|
|
|
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface; |
|
20
|
|
|
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface; |
|
21
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class BlockFixtureFactory implements FixtureFactoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var FactoryInterface */ |
|
26
|
|
|
private $blockFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** @var FactoryInterface */ |
|
29
|
|
|
private $blockTranslationFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** @var BlockRepositoryInterface */ |
|
32
|
|
|
private $blockRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** @var ProductsAssignerInterface */ |
|
35
|
|
|
private $productsAssigner; |
|
36
|
|
|
|
|
37
|
|
|
/** @var SectionsAssignerInterface */ |
|
38
|
|
|
private $sectionsAssigner; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ChannelsAssignerInterface */ |
|
41
|
|
|
private $channelAssigner; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
FactoryInterface $blockFactory, |
|
45
|
|
|
FactoryInterface $blockTranslationFactory, |
|
46
|
|
|
BlockRepositoryInterface $blockRepository, |
|
47
|
|
|
ProductsAssignerInterface $productsAssigner, |
|
48
|
|
|
SectionsAssignerInterface $sectionsAssigner, |
|
49
|
|
|
ChannelsAssignerInterface $channelAssigner |
|
50
|
|
|
) { |
|
51
|
|
|
$this->blockFactory = $blockFactory; |
|
52
|
|
|
$this->blockTranslationFactory = $blockTranslationFactory; |
|
53
|
|
|
$this->blockRepository = $blockRepository; |
|
54
|
|
|
$this->productsAssigner = $productsAssigner; |
|
55
|
|
|
$this->sectionsAssigner = $sectionsAssigner; |
|
56
|
|
|
$this->channelAssigner = $channelAssigner; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function load(array $data): void |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($data as $code => $fields) { |
|
62
|
|
|
if ( |
|
63
|
|
|
true === $fields['remove_existing'] && |
|
64
|
|
|
null !== $block = $this->blockRepository->findOneBy(['code' => $code]) |
|
65
|
|
|
) { |
|
66
|
|
|
$this->blockRepository->remove($block); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (null !== $fields['number']) { |
|
70
|
|
|
for ($i = 0; $i < $fields['number']; ++$i) { |
|
71
|
|
|
$this->createBlock(md5(uniqid()), $fields); |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->createBlock($code, $fields); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function createBlock(string $code, array $blockData): void |
|
80
|
|
|
{ |
|
81
|
|
|
/** @var BlockInterface $block */ |
|
82
|
|
|
$block = $this->blockFactory->createNew(); |
|
83
|
|
|
|
|
84
|
|
|
$this->sectionsAssigner->assign($block, $blockData['sections']); |
|
85
|
|
|
$this->productsAssigner->assign($block, $blockData['products']); |
|
86
|
|
|
$this->channelAssigner->assign($block, $blockData['channels']); |
|
87
|
|
|
|
|
88
|
|
|
$block->setCode($code); |
|
89
|
|
|
$block->setEnabled($blockData['enabled']); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($blockData['translations'] as $localeCode => $translation) { |
|
92
|
|
|
/** @var BlockTranslationInterface $blockTranslation */ |
|
93
|
|
|
$blockTranslation = $this->blockTranslationFactory->createNew(); |
|
94
|
|
|
|
|
95
|
|
|
$blockTranslation->setLocale($localeCode); |
|
96
|
|
|
$blockTranslation->setName($translation['name']); |
|
97
|
|
|
$blockTranslation->setContent($translation['content']); |
|
98
|
|
|
$blockTranslation->setLink($translation['link']); |
|
99
|
|
|
$block->addTranslation($blockTranslation); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->blockRepository->add($block); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|