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