forked from BitBagCommerce/SyliusCmsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageImporter.php
161 lines (131 loc) · 6.25 KB
/
PageImporter.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/
declare(strict_types=1);
namespace BitBag\SyliusCmsPlugin\Importer;
use BitBag\SyliusCmsPlugin\Downloader\ImageDownloaderInterface;
use BitBag\SyliusCmsPlugin\Entity\PageImage;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterChannelsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterSectionsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;
final class PageImporter extends AbstractImporter implements PageImporterInterface
{
/** @var ResourceResolverInterface */
private $pageResourceResolver;
/** @var ResourceResolverInterface */
private $sectionResolver;
/** @var LocaleContextInterface */
private $localeContext;
/** @var ImageDownloaderInterface */
private $imageDownloader;
/** @var ImageUploaderInterface */
private $imageUploader;
/** @var ImporterSectionsResolverInterface */
private $importerSectionsResolver;
/** @var ImporterChannelsResolverInterface */
private $importerChannelsResolver;
/** @var ImporterProductsResolverInterface */
private $importerProductsResolver;
/** @var EntityManagerInterface */
private $entityManager;
public function __construct(
ResourceResolverInterface $pageResourceResolver,
ResourceResolverInterface $sectionResolver,
LocaleContextInterface $localeContext,
ImageDownloaderInterface $imageDownloader,
ImageUploaderInterface $imageUploader,
ImporterSectionsResolverInterface $importerSectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ValidatorInterface $validator,
EntityManagerInterface $entityManager
) {
parent::__construct($validator);
$this->pageResourceResolver = $pageResourceResolver;
$this->sectionResolver = $sectionResolver;
$this->localeContext = $localeContext;
$this->imageDownloader = $imageDownloader;
$this->imageUploader = $imageUploader;
$this->importerSectionsResolver = $importerSectionsResolver;
$this->importerChannelsResolver = $importerChannelsResolver;
$this->importerProductsResolver = $importerProductsResolver;
$this->entityManager = $entityManager;
}
public function import(array $row): void
{
/** @var string $code */
$code = $this->getColumnValue(self::CODE_COLUMN, $row);
Assert::notNull($code);
/** @var PageInterface $page */
$page = $this->pageResourceResolver->getResource($code);
$page->setCode($code);
$page->setFallbackLocale($this->localeContext->getLocaleCode());
foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) {
$page->setCurrentLocale($locale);
$page->setSlug($this->getTranslatableColumnValue(self::SLUG_COLUMN, $locale, $row));
$page->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row));
$page->setMetaKeywords($this->getTranslatableColumnValue(self::META_KEYWORDS_COLUMN, $locale, $row));
$page->setMetaDescription($this->getTranslatableColumnValue(self::META_DESCRIPTION_COLUMN, $locale, $row));
$page->setContent($this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row));
$page->setBreadcrumb($this->getTranslatableColumnValue(self::BREADCRUMB_COLUMN, $locale, $row));
$page->setNameWhenLinked($this->getTranslatableColumnValue(self::NAME_WHEN_LINKED_COLUMN, $locale, $row));
$page->setDescriptionWhenLinked($this->getTranslatableColumnValue(self::DESCRIPTION_WHEN_LINKED_COLUMN, $locale, $row));
$url = $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row);
if (null !== $url) {
$this->resolveImage($page, $url ?? '', $locale);
}
}
$this->importerSectionsResolver->resolve($page, $this->getColumnValue(self::SECTIONS_COLUMN, $row));
$this->importerChannelsResolver->resolve($page, $this->getColumnValue(self::CHANNELS_COLUMN, $row));
$this->importerProductsResolver->resolve($page, $this->getColumnValue(self::PRODUCTS_COLUMN, $row));
$this->validateResource($page, ['bitbag']);
$page->getId() ?: $this->entityManager->persist($page);
$this->entityManager->flush();
}
public function getResourceCode(): string
{
return 'page';
}
private function resolveImage(PageInterface $page, string $url, string $locale): void
{
/** @var PageTranslationInterface $pageTranslation */
$pageTranslation = $page->getTranslation($locale);
$downloadedImage = $this->imageDownloader->download($url);
if (null !== $pageImage = $pageTranslation->getImage()) {
$this->imageUploader->remove($pageTranslation->getImage()->getPath());
} else {
$pageImage = new PageImage();
}
$pageImage->setFile($downloadedImage);
$pageImage->setOwner($pageTranslation);
$this->imageUploader->upload($pageImage);
$this->entityManager->persist($pageImage);
}
private function getTranslatableColumns(): array
{
return [
self::SLUG_COLUMN,
self::NAME_COLUMN,
self::IMAGE_COLUMN,
self::META_KEYWORDS_COLUMN,
self::META_DESCRIPTION_COLUMN,
self::CONTENT_COLUMN,
self::BREADCRUMB_COLUMN,
self::NAME_WHEN_LINKED_COLUMN,
self::DESCRIPTION_WHEN_LINKED_COLUMN,
];
}
}