Completed
Pull Request — master (#169)
by Mikołaj
02:27
created

BlockImporter::getTranslatableColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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