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