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