Completed
Push — master ( cf353c...bbc3bb )
by Mikołaj
11s
created

MediaImporter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

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