Completed
Pull Request — master (#169)
by Mikołaj
03:46
created

BlockImporter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

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