-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathMediaImporter.php
More file actions
75 lines (62 loc) · 2.87 KB
/
Copy pathMediaImporter.php
File metadata and controls
75 lines (62 loc) · 2.87 KB
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
<?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
{
public function __construct(
private ResourceResolverInterface $mediaResourceResolver,
private LocaleContextInterface $localeContext,
private ImporterSectionsResolverInterface $importerSectionsResolver,
private ImporterProductsResolverInterface $importerProductsResolver,
ValidatorInterface $validator,
private MediaRepositoryInterface $mediaRepository,
) {
parent::__construct($validator);
}
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,
];
}
}