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\BlockInterface; |
16
|
|
|
use BitBag\CmsPlugin\Entity\BlockTranslationInterface; |
17
|
|
|
use BitBag\CmsPlugin\Entity\BlockImage; |
18
|
|
|
use BitBag\CmsPlugin\Factory\BlockFactoryInterface; |
19
|
|
|
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface; |
20
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
21
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface; |
22
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
23
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
24
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
25
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Mikołaj Król <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class BlockFixture extends AbstractFixture implements FixtureInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var BlockFactoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $blockFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FactoryInterface |
39
|
|
|
*/ |
40
|
|
|
private $blockTranslationFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var BlockRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
private $blockRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ImageUploaderInterface |
49
|
|
|
*/ |
50
|
|
|
private $imageUploader; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param BlockFactoryInterface $blockFactory |
54
|
|
|
* @param FactoryInterface $blockTranslationFactory |
55
|
|
|
* @param BlockRepositoryInterface $blockRepository |
56
|
|
|
* @param ImageUploaderInterface $imageUploader |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
BlockFactoryInterface $blockFactory, |
60
|
|
|
FactoryInterface $blockTranslationFactory, |
61
|
|
|
BlockRepositoryInterface $blockRepository, |
62
|
|
|
ImageUploaderInterface $imageUploader |
63
|
|
|
) |
64
|
|
|
{ |
65
|
|
|
$this->blockFactory = $blockFactory; |
66
|
|
|
$this->blockTranslationFactory = $blockTranslationFactory; |
67
|
|
|
$this->blockRepository = $blockRepository; |
68
|
|
|
$this->imageUploader = $imageUploader; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function load(array $options): void |
75
|
|
|
{ |
76
|
|
|
foreach ($options['blocks'] as $code => $fields) { |
77
|
|
|
|
78
|
|
|
if (null !== $this->blockRepository->findOneBy(['code' => $code])) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$type = $fields['type']; |
83
|
|
|
$block = $this->blockFactory->createWithType($type); |
84
|
|
|
|
85
|
|
|
$block->setCode($code); |
86
|
|
|
$block->setEnabled($fields['enabled']); |
87
|
|
|
|
88
|
|
|
foreach ($fields['translations'] as $localeCode => $translation) { |
89
|
|
|
/** @var BlockTranslationInterface $blockTranslation */ |
90
|
|
|
$blockTranslation = $this->blockTranslationFactory->createNew(); |
91
|
|
|
|
92
|
|
|
$blockTranslation->setLocale($localeCode); |
93
|
|
|
$blockTranslation->setName($translation['name']); |
94
|
|
|
$blockTranslation->setContent($translation['content']); |
95
|
|
|
|
96
|
|
|
if (BlockInterface::IMAGE_BLOCK_TYPE === $type) { |
97
|
|
|
$image = new BlockImage(); |
98
|
|
|
$path = $translation['image_path']; |
99
|
|
|
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg'); |
100
|
|
|
|
101
|
|
|
$image->setFile($uploadedImage); |
102
|
|
|
$blockTranslation->setImage($image); |
103
|
|
|
|
104
|
|
|
$this->imageUploader->upload($image); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$block->addTranslation($blockTranslation); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->blockRepository->add($block); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function getName(): string |
118
|
|
|
{ |
119
|
|
|
return 'bitbag_cms_block'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void |
126
|
|
|
{ |
127
|
|
|
$optionsNode |
128
|
|
|
->children() |
129
|
|
|
->arrayNode('blocks') |
130
|
|
|
->prototype('array') |
131
|
|
|
->children() |
132
|
|
|
->scalarNode('type')->isRequired()->cannotBeEmpty()->end() |
133
|
|
|
->scalarNode('enabled')->defaultTrue()->end() |
134
|
|
|
->arrayNode('translations') |
135
|
|
|
->prototype('array') |
136
|
|
|
->children() |
137
|
|
|
->scalarNode('name')->defaultNull()->end() |
138
|
|
|
->scalarNode('content')->defaultNull()->end() |
139
|
|
|
->scalarNode('link')->defaultNull()->end() |
140
|
|
|
->scalarNode('image_path')->defaultNull()->end() |
141
|
|
|
->end() |
142
|
|
|
->end() |
143
|
|
|
->end() |
144
|
|
|
->end() |
145
|
|
|
->end() |
146
|
|
|
->end() |
147
|
|
|
->end() |
148
|
|
|
; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|