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