forked from BitBagCommerce/SyliusCmsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaImporter.php
96 lines (77 loc) · 3.52 KB
/
MediaImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* 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
*/
declare(strict_types=1);
namespace BitBag\SyliusCmsPlugin\Importer;
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterSectionsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;
final class MediaImporter extends AbstractImporter implements MediaImporterInterface
{
/** @var ResourceResolverInterface */
private $mediaResourceResolver;
/** @var LocaleContextInterface */
private $localeContext;
/** @var ImporterSectionsResolverInterface */
private $importerSectionsResolver;
/** @var ImporterProductsResolverInterface */
private $importerProductsResolver;
/** @var MediaRepositoryInterface */
private $mediaRepository;
public function __construct(
ResourceResolverInterface $mediaResourceResolver,
LocaleContextInterface $localeContext,
ImporterSectionsResolverInterface $importerSectionsResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ValidatorInterface $validator,
MediaRepositoryInterface $mediaRepository
) {
parent::__construct($validator);
$this->mediaResourceResolver = $mediaResourceResolver;
$this->localeContext = $localeContext;
$this->importerSectionsResolver = $importerSectionsResolver;
$this->importerProductsResolver = $importerProductsResolver;
$this->mediaRepository = $mediaRepository;
}
public function import(array $row): void
{
/** @var string|null $code */
$code = $this->getColumnValue(self::CODE_COLUMN, $row);
Assert::notNull($code);
/** @var MediaInterface $media */
$media = $this->mediaResourceResolver->getResource($code);
$media->setCode($code);
$media->setType($this->getColumnValue(self::TYPE_COLUMN, $row));
$media->setFallbackLocale($this->localeContext->getLocaleCode());
foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) {
$media->setCurrentLocale($locale);
$media->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row));
$media->setContent($this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row));
$media->setAlt($this->getTranslatableColumnValue(self::ALT_COLUMN, $locale, $row));
}
$this->importerSectionsResolver->resolve($media, $this->getColumnValue(self::SECTIONS_COLUMN, $row));
$this->importerProductsResolver->resolve($media, $this->getColumnValue(self::PRODUCTS_COLUMN, $row));
$this->validateResource($media, ['bitbag']);
$this->mediaRepository->add($media);
}
public function getResourceCode(): string
{
return 'media';
}
private function getTranslatableColumns(): array
{
return [
self::NAME_COLUMN,
self::CONTENT_COLUMN,
self::ALT_COLUMN,
];
}
}