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\ProductsAssignerInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaTranslationInterface; |
19
|
|
|
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; |
20
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
21
|
|
|
|
22
|
|
|
final class MediaFixtureFactory implements FixtureFactoryInterface |
23
|
|
|
{ |
24
|
|
|
/** @var FactoryInterface */ |
25
|
|
|
private $mediaFactory; |
26
|
|
|
|
27
|
|
|
/** @var FactoryInterface */ |
28
|
|
|
private $mediaTranslationFactory; |
29
|
|
|
|
30
|
|
|
/** @var MediaRepositoryInterface */ |
31
|
|
|
private $mediaRepository; |
32
|
|
|
|
33
|
|
|
/** @var ProductsAssignerInterface */ |
34
|
|
|
private $productsAssigner; |
35
|
|
|
|
36
|
|
|
/** @var SectionsAssignerInterface */ |
37
|
|
|
private $sectionsAssigner; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
FactoryInterface $mediaFactory, |
41
|
|
|
FactoryInterface $mediaTranslationFactory, |
42
|
|
|
MediaRepositoryInterface $mediaRepository, |
43
|
|
|
ProductsAssignerInterface $productsAssigner, |
44
|
|
|
SectionsAssignerInterface $sectionsAssigner |
45
|
|
|
) { |
46
|
|
|
$this->mediaFactory = $mediaFactory; |
47
|
|
|
$this->mediaTranslationFactory = $mediaTranslationFactory; |
48
|
|
|
$this->mediaRepository = $mediaRepository; |
49
|
|
|
$this->productsAssigner = $productsAssigner; |
50
|
|
|
$this->sectionsAssigner = $sectionsAssigner; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function load(array $data): void |
54
|
|
|
{ |
55
|
|
|
foreach ($data as $code => $fields) { |
56
|
|
|
if ( |
57
|
|
|
true === $fields['remove_existing'] && |
58
|
|
|
null !== $media = $this->mediaRepository->findOneBy(['code' => $code]) |
59
|
|
|
) { |
60
|
|
|
$this->mediaRepository->remove($media); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (null !== $fields['number']) { |
64
|
|
|
for ($i = 0; $i < $fields['number']; ++$i) { |
65
|
|
|
$this->createMedia(md5(uniqid()), $fields); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
$this->createMedia($code, $fields); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function createMedia(string $code, array $mediaData): void |
74
|
|
|
{ |
75
|
|
|
/** @var MediaInterface $media */ |
76
|
|
|
$media = $this->mediaFactory->createNew(); |
77
|
|
|
$media->setType($mediaData['type']); |
78
|
|
|
$media->setCode($code); |
79
|
|
|
$media->setEnabled($mediaData['enabled']); |
80
|
|
|
|
81
|
|
|
foreach ($mediaData['translations'] as $localeCode => $translation) { |
82
|
|
|
/** @var MediaTranslationInterface $mediaTranslation */ |
83
|
|
|
$mediaTranslation = $this->mediaTranslationFactory->createNew(); |
84
|
|
|
|
85
|
|
|
$mediaTranslation->setLocale($localeCode); |
86
|
|
|
$mediaTranslation->setName($translation['name']); |
87
|
|
|
$mediaTranslation->setDescription($translation['description']); |
88
|
|
|
$mediaTranslation->setAlt($translation['alt']); |
89
|
|
|
$media->addTranslation($mediaTranslation); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->sectionsAssigner->assign($media, $mediaData['sections']); |
93
|
|
|
$this->productsAssigner->assign($media, $mediaData['products']); |
94
|
|
|
|
95
|
|
|
$this->mediaRepository->add($media); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|