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\Downloader\ImageDownloaderInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageContentInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageImage; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface; |
19
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ImporterChannelsResolverInterface; |
20
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface; |
21
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ImporterSectionsResolverInterface; |
22
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface; |
23
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
24
|
|
|
use Sylius\Component\Core\Uploader\ImageUploader; |
25
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
26
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
27
|
|
|
use Webmozart\Assert\Assert; |
28
|
|
|
|
29
|
|
|
final class PageImporter extends AbstractImporter implements PageImporterInterface |
30
|
|
|
{ |
31
|
|
|
/** @var ResourceResolverInterface */ |
32
|
|
|
private $pageResourceResolver; |
33
|
|
|
|
34
|
|
|
/** @var ResourceResolverInterface */ |
35
|
|
|
private $sectionResolver; |
36
|
|
|
|
37
|
|
|
/** @var LocaleContextInterface */ |
38
|
|
|
private $localeContext; |
39
|
|
|
|
40
|
|
|
/** @var ImageDownloaderInterface */ |
41
|
|
|
private $imageDownloader; |
42
|
|
|
|
43
|
|
|
/** @var ImageUploader */ |
44
|
|
|
private $imageUploader; |
45
|
|
|
|
46
|
|
|
/** @var ImporterSectionsResolverInterface */ |
47
|
|
|
private $importerSectionsResolver; |
48
|
|
|
|
49
|
|
|
/** @var ImporterChannelsResolverInterface */ |
50
|
|
|
private $importerChannelsResolver; |
51
|
|
|
|
52
|
|
|
/** @var ImporterProductsResolverInterface */ |
53
|
|
|
private $importerProductsResolver; |
54
|
|
|
|
55
|
|
|
/** @var EntityManagerInterface */ |
56
|
|
|
private $entityManager; |
57
|
|
|
|
58
|
|
|
public function __construct( |
59
|
|
|
ResourceResolverInterface $pageResourceResolver, |
60
|
|
|
ResourceResolverInterface $sectionResolver, |
61
|
|
|
LocaleContextInterface $localeContext, |
62
|
|
|
ImageDownloaderInterface $imageDownloader, |
63
|
|
|
ImageUploader $imageUploader, |
64
|
|
|
ImporterSectionsResolverInterface $importerSectionsResolver, |
65
|
|
|
ImporterChannelsResolverInterface $importerChannelsResolver, |
66
|
|
|
ImporterProductsResolverInterface $importerProductsResolver, |
67
|
|
|
ValidatorInterface $validator, |
68
|
|
|
EntityManagerInterface $entityManager |
69
|
|
|
) { |
70
|
|
|
parent::__construct($validator); |
71
|
|
|
|
72
|
|
|
$this->pageResourceResolver = $pageResourceResolver; |
73
|
|
|
$this->sectionResolver = $sectionResolver; |
74
|
|
|
$this->localeContext = $localeContext; |
75
|
|
|
$this->imageDownloader = $imageDownloader; |
76
|
|
|
$this->imageUploader = $imageUploader; |
77
|
|
|
$this->importerSectionsResolver = $importerSectionsResolver; |
78
|
|
|
$this->importerChannelsResolver = $importerChannelsResolver; |
79
|
|
|
$this->importerProductsResolver = $importerProductsResolver; |
80
|
|
|
$this->entityManager = $entityManager; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function import(array $row): void |
84
|
|
|
{ |
85
|
|
|
/** @var string $code */ |
86
|
|
|
$code = $this->getColumnValue(self::CODE_COLUMN, $row); |
87
|
|
|
Assert::notNull($code); |
88
|
|
|
|
89
|
|
|
/** @var PageContentInterface $page */ |
90
|
|
|
$page = $this->pageResourceResolver->getResource($code); |
91
|
|
|
|
92
|
|
|
$page->setCode($code); |
93
|
|
|
$page->setFallbackLocale($this->localeContext->getLocaleCode()); |
94
|
|
|
|
95
|
|
|
foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) { |
96
|
|
|
$page->setCurrentLocale($locale); |
97
|
|
|
$page->setSlug($this->getTranslatableColumnValue(self::SLUG_COLUMN, $locale, $row)); |
98
|
|
|
$page->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row)); |
99
|
|
|
$page->setMetaKeywords($this->getTranslatableColumnValue(self::META_KEYWORDS_COLUMN, $locale, $row)); |
100
|
|
|
$page->setMetaDescription($this->getTranslatableColumnValue(self::META_DESCRIPTION_COLUMN, $locale, $row)); |
101
|
|
|
$page->setContent($this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row)); |
102
|
|
|
$page->setBreadcrumb($this->getTranslatableColumnValue(self::BREADCRUMB_COLUMN, $locale, $row)); |
103
|
|
|
$page->setNameWhenLinked($this->getTranslatableColumnValue(self::NAME_WHEN_LINKED_COLUMN, $locale, $row)); |
104
|
|
|
$page->setDescriptionWhenLinked($this->getTranslatableColumnValue(self::DESCRIPTION_WHEN_LINKED_COLUMN, $locale, $row)); |
105
|
|
|
|
106
|
|
|
$url = $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row); |
107
|
|
|
|
108
|
|
|
if (null !== $url) { |
109
|
|
|
$this->resolveImage($page, $url ?? '', $locale); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->importerSectionsResolver->resolve($page, $this->getColumnValue(self::SECTIONS_COLUMN, $row)); |
114
|
|
|
$this->importerChannelsResolver->resolve($page, $this->getColumnValue(self::CHANNELS_COLUMN, $row)); |
115
|
|
|
$this->importerProductsResolver->resolve($page, $this->getColumnValue(self::PRODUCTS_COLUMN, $row)); |
116
|
|
|
|
117
|
|
|
$this->validateResource($page, ['bitbag']); |
118
|
|
|
|
119
|
|
|
$page->getId() ?: $this->entityManager->persist($page); |
120
|
|
|
$this->entityManager->flush(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getResourceCode(): string |
124
|
|
|
{ |
125
|
|
|
return 'page'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function resolveImage(PageContentInterface $page, string $url, string $locale): void |
129
|
|
|
{ |
130
|
|
|
/** @var PageTranslationInterface $pageTranslation */ |
131
|
|
|
$pageTranslation = $page->getTranslation($locale); |
132
|
|
|
$downloadedImage = $this->imageDownloader->download($url); |
133
|
|
|
|
134
|
|
|
if (null !== $pageImage = $pageTranslation->getImage()) { |
135
|
|
|
$this->imageUploader->remove($pageTranslation->getImage()->getPath()); |
136
|
|
|
} else { |
137
|
|
|
$pageImage = new PageImage(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$pageImage->setFile($downloadedImage); |
141
|
|
|
$pageImage->setOwner($pageTranslation); |
142
|
|
|
|
143
|
|
|
$this->imageUploader->upload($pageImage); |
144
|
|
|
$this->entityManager->persist($pageImage); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function getTranslatableColumns(): array |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
|
|
self::SLUG_COLUMN, |
151
|
|
|
self::NAME_COLUMN, |
152
|
|
|
self::IMAGE_COLUMN, |
153
|
|
|
self::META_KEYWORDS_COLUMN, |
154
|
|
|
self::META_DESCRIPTION_COLUMN, |
155
|
|
|
self::CONTENT_COLUMN, |
156
|
|
|
self::BREADCRUMB_COLUMN, |
157
|
|
|
self::NAME_WHEN_LINKED_COLUMN, |
158
|
|
|
self::DESCRIPTION_WHEN_LINKED_COLUMN, |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|