|
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\Importer; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
|
14
|
|
|
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; |
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface; |
|
16
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ImporterSectionsResolverInterface; |
|
17
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface; |
|
18
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
19
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
final class MediaImporter extends AbstractImporter implements MediaImporterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ResourceResolverInterface */ |
|
25
|
|
|
private $mediaResourceResolver; |
|
26
|
|
|
|
|
27
|
|
|
/** @var LocaleContextInterface */ |
|
28
|
|
|
private $localeContext; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ImporterSectionsResolverInterface */ |
|
31
|
|
|
private $importerSectionsResolver; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ImporterProductsResolverInterface */ |
|
34
|
|
|
private $importerProductsResolver; |
|
35
|
|
|
|
|
36
|
|
|
/** @var MediaRepositoryInterface */ |
|
37
|
|
|
private $mediaRepository; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
ResourceResolverInterface $mediaResourceResolver, |
|
41
|
|
|
LocaleContextInterface $localeContext, |
|
42
|
|
|
ImporterSectionsResolverInterface $importerSectionsResolver, |
|
43
|
|
|
ImporterProductsResolverInterface $importerProductsResolver, |
|
44
|
|
|
ValidatorInterface $validator, |
|
45
|
|
|
MediaRepositoryInterface $mediaRepository |
|
46
|
|
|
) { |
|
47
|
|
|
parent::__construct($validator); |
|
48
|
|
|
|
|
49
|
|
|
$this->mediaResourceResolver = $mediaResourceResolver; |
|
50
|
|
|
$this->localeContext = $localeContext; |
|
51
|
|
|
$this->importerSectionsResolver = $importerSectionsResolver; |
|
52
|
|
|
$this->importerProductsResolver = $importerProductsResolver; |
|
53
|
|
|
$this->mediaRepository = $mediaRepository; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function import(array $row): void |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var string|null $code */ |
|
59
|
|
|
$code = $this->getColumnValue(self::CODE_COLUMN, $row); |
|
60
|
|
|
Assert::notNull($code); |
|
61
|
|
|
/** @var MediaInterface $media */ |
|
62
|
|
|
$media = $this->mediaResourceResolver->getResource($code); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$media->setCode($code); |
|
65
|
|
|
$media->setType($this->getColumnValue(self::TYPE_COLUMN, $row)); |
|
66
|
|
|
$media->setFallbackLocale($this->localeContext->getLocaleCode()); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) { |
|
69
|
|
|
$media->setCurrentLocale($locale); |
|
70
|
|
|
$media->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row)); |
|
71
|
|
|
$media->setContent($this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row)); |
|
72
|
|
|
$media->setAlt($this->getTranslatableColumnValue(self::ALT_COLUMN, $locale, $row)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->importerSectionsResolver->resolve($media, $this->getColumnValue(self::SECTIONS_COLUMN, $row)); |
|
76
|
|
|
$this->importerProductsResolver->resolve($media, $this->getColumnValue(self::PRODUCTS_COLUMN, $row)); |
|
77
|
|
|
|
|
78
|
|
|
$this->validateResource($media, ['bitbag']); |
|
79
|
|
|
|
|
80
|
|
|
$this->mediaRepository->add($media); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getResourceCode(): string |
|
84
|
|
|
{ |
|
85
|
|
|
return 'media'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function getTranslatableColumns(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
self::NAME_COLUMN, |
|
92
|
|
|
self::CONTENT_COLUMN, |
|
93
|
|
|
self::ALT_COLUMN, |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|