1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Fixture\Factory; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface; |
14
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface; |
15
|
|
|
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaTranslationInterface; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; |
19
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface; |
20
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
22
|
|
|
|
23
|
|
|
final class MediaFixtureFactory implements FixtureFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** @var FactoryInterface */ |
26
|
|
|
private $mediaFactory; |
27
|
|
|
|
28
|
|
|
/** @var FactoryInterface */ |
29
|
|
|
private $mediaTranslationFactory; |
30
|
|
|
|
31
|
|
|
/** @var MediaProviderResolverInterface */ |
32
|
|
|
private $mediaProviderResolver; |
33
|
|
|
|
34
|
|
|
/** @var MediaRepositoryInterface */ |
35
|
|
|
private $mediaRepository; |
36
|
|
|
|
37
|
|
|
/** @var ProductsAssignerInterface */ |
38
|
|
|
private $productsAssigner; |
39
|
|
|
|
40
|
|
|
/** @var SectionsAssignerInterface */ |
41
|
|
|
private $sectionsAssigner; |
42
|
|
|
|
43
|
|
|
/** @var ChannelsAssignerInterface */ |
44
|
|
|
private $channelAssigner; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
FactoryInterface $mediaFactory, |
48
|
|
|
FactoryInterface $mediaTranslationFactory, |
49
|
|
|
MediaProviderResolverInterface $mediaProviderResolver, |
50
|
|
|
MediaRepositoryInterface $mediaRepository, |
51
|
|
|
ProductsAssignerInterface $productsAssigner, |
52
|
|
|
SectionsAssignerInterface $sectionsAssigner, |
53
|
|
|
ChannelsAssignerInterface $channelAssigner |
54
|
|
|
) { |
55
|
|
|
$this->mediaFactory = $mediaFactory; |
56
|
|
|
$this->mediaTranslationFactory = $mediaTranslationFactory; |
57
|
|
|
$this->mediaProviderResolver = $mediaProviderResolver; |
58
|
|
|
$this->mediaRepository = $mediaRepository; |
59
|
|
|
$this->productsAssigner = $productsAssigner; |
60
|
|
|
$this->sectionsAssigner = $sectionsAssigner; |
61
|
|
|
$this->channelAssigner = $channelAssigner; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function load(array $data): void |
65
|
|
|
{ |
66
|
|
|
foreach ($data as $code => $fields) { |
67
|
|
|
if ( |
68
|
|
|
true === $fields['remove_existing'] && |
69
|
|
|
null !== $media = $this->mediaRepository->findOneBy(['code' => $code]) |
70
|
|
|
) { |
71
|
|
|
$this->mediaRepository->remove($media); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (null !== $fields['number']) { |
75
|
|
|
for ($i = 0; $i < $fields['number']; ++$i) { |
76
|
|
|
$this->createMedia(md5(uniqid()), $fields); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$this->createMedia($code, $fields); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function createMedia(string $code, array $mediaData): void |
85
|
|
|
{ |
86
|
|
|
/** @var MediaInterface $media */ |
87
|
|
|
$media = $this->mediaFactory->createNew(); |
88
|
|
|
$media->setType($mediaData['type']); |
89
|
|
|
$media->setCode($code); |
90
|
|
|
$media->setEnabled($mediaData['enabled']); |
91
|
|
|
$media->setFile(new File($mediaData['path'])); |
92
|
|
|
|
93
|
|
|
$this->mediaProviderResolver->resolveProvider($media)->upload($media); |
94
|
|
|
|
95
|
|
|
foreach ($mediaData['translations'] as $localeCode => $translation) { |
96
|
|
|
/** @var MediaTranslationInterface $mediaTranslation */ |
97
|
|
|
$mediaTranslation = $this->mediaTranslationFactory->createNew(); |
98
|
|
|
|
99
|
|
|
$mediaTranslation->setLocale($localeCode); |
100
|
|
|
$mediaTranslation->setName($translation['name']); |
101
|
|
|
$mediaTranslation->setContent($translation['content']); |
102
|
|
|
$mediaTranslation->setAlt($translation['alt']); |
103
|
|
|
$mediaTranslation->setLink($translation['link']); |
104
|
|
|
$media->addTranslation($mediaTranslation); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->sectionsAssigner->assign($media, $mediaData['sections']); |
108
|
|
|
$this->productsAssigner->assign($media, $mediaData['productCodes']); |
109
|
|
|
$this->channelAssigner->assign($media, $mediaData['channels']); |
110
|
|
|
|
111
|
|
|
$this->mediaRepository->add($media); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|