forked from BitBagCommerce/SyliusCmsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockImporter.php
101 lines (82 loc) · 3.85 KB
/
BlockImporter.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
97
98
99
100
101
<?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\BlockInterface;
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterChannelsResolverInterface;
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 BlockImporter extends AbstractImporter implements BlockImporterInterface
{
/** @var ResourceResolverInterface */
private $blockResourceResolver;
/** @var LocaleContextInterface */
private $localeContext;
/** @var ImporterSectionsResolverInterface */
private $importerSectionsResolver;
/** @var ImporterChannelsResolverInterface */
private $importerChannelsResolver;
/** @var ImporterProductsResolverInterface */
private $importerProductsResolver;
/** @var BlockRepositoryInterface */
private $blockRepository;
public function __construct(
ResourceResolverInterface $blockResourceResolver,
LocaleContextInterface $localeContext,
ImporterSectionsResolverInterface $importerSectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ValidatorInterface $validator,
BlockRepositoryInterface $blockRepository
) {
parent::__construct($validator);
$this->blockResourceResolver = $blockResourceResolver;
$this->localeContext = $localeContext;
$this->importerSectionsResolver = $importerSectionsResolver;
$this->importerChannelsResolver = $importerChannelsResolver;
$this->importerProductsResolver = $importerProductsResolver;
$this->blockRepository = $blockRepository;
}
public function import(array $row): void
{
/** @var string|null $code */
$code = $this->getColumnValue(self::CODE_COLUMN, $row);
Assert::notNull($code);
/** @var BlockInterface $block */
$block = $this->blockResourceResolver->getResource($code);
$block->setCode($code);
$block->setFallbackLocale($this->localeContext->getLocaleCode());
foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) {
$block->setCurrentLocale($locale);
$block->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row));
$block->setLink($this->getTranslatableColumnValue(self::LINK_COLUMN, $locale, $row));
$block->setContent($this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row));
}
$this->importerSectionsResolver->resolve($block, $this->getColumnValue(self::SECTIONS_COLUMN, $row));
$this->importerChannelsResolver->resolve($block, $this->getColumnValue(self::CHANNELS_COLUMN, $row));
$this->importerProductsResolver->resolve($block, $this->getColumnValue(self::PRODUCTS_COLUMN, $row));
$this->validateResource($block, ['bitbag']);
$this->blockRepository->add($block);
}
public function getResourceCode(): string
{
return 'block';
}
private function getTranslatableColumns(): array
{
return [
self::NAME_COLUMN,
self::CONTENT_COLUMN,
self::LINK_COLUMN,
];
}
}