Skip to content

Commit 3053f4f

Browse files
authored
feat: Ibexa Taxonomy writer (#221)
Co-authored-by: Florian ALEXANDRE <f.alexandre@novactive.com>
2 parents db6f211 + aadc525 commit 3053f4f

File tree

9 files changed

+484
-1
lines changed

9 files changed

+484
-1
lines changed

components/ImportExportBundle/src/bundle/DependencyInjection/AlmaviaCXIbexaImportExportExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function load(array $configs, ContainerBuilder $container): void
3737
}
3838
if (in_array('IbexaTaxonomyBundle', $activatedBundles, true)) {
3939
$loader->load('item_value_transformer/taxonomy.yaml');
40+
$loader->load('taxonomy_services.yaml');
4041
}
4142
// if (in_array('IbexaFormBuilderBundle', $activatedBundles, true)) {
4243
// }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
services:
2+
3+
AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyWriter:
4+
parent: AlmaviaCX\Bundle\IbexaImportExport\Writer\AbstractWriter
5+
arguments:
6+
$repository: '@Ibexa\Contracts\Core\Repository\Repository'
7+
$taxonomyAccessorBuilder: '@AlmaviaCX\Bundle\IbexaImportExport\Accessor\Ibexa\Taxonomy\TaxonomyAccessorBuilder'
8+
$taxonomyImporter: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyImporter'
9+
tags:
10+
- { name: almaviacx.import_export.component, alias: writer.ibexa.content }
11+
12+
AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyCreator:
13+
arguments:
14+
$contentCreator: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentCreator'
15+
$taxonomyService: '@Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface'
16+
$taxonomyConfiguration: '@Ibexa\Taxonomy\Service\TaxonomyConfiguration'
17+
18+
AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyUpdater:
19+
arguments:
20+
$contentUpdater: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentUpdater'
21+
$taxonomyService: '@Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface'
22+
$taxonomyConfiguration: '@Ibexa\Taxonomy\Service\TaxonomyConfiguration'
23+
24+
AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyImporter:
25+
arguments:
26+
$repository: '@Ibexa\Contracts\Core\Repository\Repository'
27+
$taxonomyService: '@Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface'
28+
$taxonomyCreator: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyCreator'
29+
$taxonomyUpdater: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy\IbexaTaxonomyUpdater'

components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentImporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __invoke(IbexaContentData $contentData, bool $allowUpdate = true
7575
);
7676
}
7777
} catch (\Throwable $exception) {
78-
dump($exception);
78+
dump($exception, $contentData);
7979
throw $exception;
8080
}
8181
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy;
6+
7+
use AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentCreator;
8+
use Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface;
9+
use Ibexa\Contracts\Taxonomy\Value\TaxonomyEntry;
10+
use Ibexa\Core\FieldType\TextLine\Value as TextLineValue;
11+
use Ibexa\Taxonomy\FieldType\TaxonomyEntry\Value as TaxonomyEntryValue;
12+
use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
13+
14+
class IbexaTaxonomyCreator
15+
{
16+
protected TaxonomyServiceInterface $taxonomyService;
17+
protected TaxonomyConfiguration $taxonomyConfiguration;
18+
protected IbexaContentCreator $contentCreator;
19+
20+
public function __construct(
21+
TaxonomyServiceInterface $taxonomyService,
22+
TaxonomyConfiguration $taxonomyConfiguration,
23+
IbexaContentCreator $contentCreator,
24+
) {
25+
$this->contentCreator = $contentCreator;
26+
$this->taxonomyConfiguration = $taxonomyConfiguration;
27+
$this->taxonomyService = $taxonomyService;
28+
}
29+
30+
/**
31+
* @param null $modificationDate
32+
*
33+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
34+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException
35+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException
36+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
37+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
38+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
39+
* @throws \Ibexa\Taxonomy\Exception\TaxonomyConfigurationNotFoundException
40+
* @throws \Ibexa\Taxonomy\Exception\TaxonomyEntryNotFoundException
41+
* @throws \Ibexa\Taxonomy\Exception\TaxonomyNotFoundException
42+
*/
43+
public function __invoke(
44+
string $identifier,
45+
TaxonomyEntry $parent,
46+
array $names,
47+
string $remoteId,
48+
int $ownerId = null,
49+
string $mainLanguageCode = 'eng-GB',
50+
int $sectionId = null,
51+
$modificationDate = null,
52+
bool $hidden = false
53+
): TaxonomyEntry {
54+
$contentTypeIdentifier = $this->taxonomyConfiguration->getConfigForTaxonomy(
55+
$parent->taxonomy,
56+
TaxonomyConfiguration::CONFIG_CONTENT_TYPE
57+
);
58+
59+
$fields = [];
60+
foreach ($names as $languageCode => $name) {
61+
$fieldsForLanguage = [
62+
'name' => new TextLineValue($name),
63+
];
64+
65+
if ('fre-FR' === $languageCode) {
66+
$fieldsForLanguage['identifier'] = new TextLineValue($identifier);
67+
$fieldsForLanguage['parent'] = new TaxonomyEntryValue($parent);
68+
}
69+
70+
$fields[$languageCode] = $fieldsForLanguage;
71+
}
72+
73+
$content = ($this->contentCreator)(
74+
$contentTypeIdentifier,
75+
[$parent->content->contentInfo->getMainLocation()],
76+
$fields,
77+
$remoteId,
78+
$ownerId,
79+
$mainLanguageCode,
80+
$sectionId,
81+
$modificationDate,
82+
$hidden
83+
);
84+
85+
return $this->taxonomyService->loadEntryByContentId($content->id);
86+
}
87+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy;
6+
7+
use DateTime;
8+
9+
class IbexaTaxonomyData
10+
{
11+
protected string $identifier;
12+
protected string $parentIdentifier;
13+
/** @var array<string, mixed> */
14+
protected array $names = [];
15+
protected string $taxonomyName;
16+
protected string $contentRemoteId;
17+
protected ?int $ownerId = null;
18+
protected ?int $sectionId = null;
19+
protected int|null|DateTime $modificationDate = null;
20+
protected string $mainLanguageCode = 'eng-GB';
21+
22+
public function getIdentifier(): string
23+
{
24+
return $this->identifier;
25+
}
26+
27+
public function setIdentifier(string $identifier): void
28+
{
29+
$this->identifier = $identifier;
30+
}
31+
32+
public function getParentIdentifier(): string
33+
{
34+
return $this->parentIdentifier;
35+
}
36+
37+
public function setParentIdentifier(string $parentIdentifier): void
38+
{
39+
$this->parentIdentifier = $parentIdentifier;
40+
}
41+
42+
public function getNames(): array
43+
{
44+
return $this->names;
45+
}
46+
47+
public function setNames(array $names): void
48+
{
49+
$this->names = $names;
50+
}
51+
52+
public function getTaxonomyName(): string
53+
{
54+
return $this->taxonomyName;
55+
}
56+
57+
public function setTaxonomyName(string $taxonomyName): void
58+
{
59+
$this->taxonomyName = $taxonomyName;
60+
}
61+
62+
public function getContentRemoteId(): string
63+
{
64+
return $this->contentRemoteId;
65+
}
66+
67+
public function setContentRemoteId(string $contentRemoteId): void
68+
{
69+
$this->contentRemoteId = $contentRemoteId;
70+
}
71+
72+
public function getOwnerId(): ?int
73+
{
74+
return $this->ownerId;
75+
}
76+
77+
public function setOwnerId(?int $ownerId): void
78+
{
79+
$this->ownerId = $ownerId;
80+
}
81+
82+
public function getSectionId(): ?int
83+
{
84+
return $this->sectionId;
85+
}
86+
87+
public function setSectionId(?int $sectionId): void
88+
{
89+
$this->sectionId = $sectionId;
90+
}
91+
92+
public function getModificationDate(): DateTime|int|null
93+
{
94+
return $this->modificationDate;
95+
}
96+
97+
public function setModificationDate(DateTime|int|null $modificationDate): void
98+
{
99+
$this->modificationDate = $modificationDate;
100+
}
101+
102+
public function getMainLanguageCode(): string
103+
{
104+
return $this->mainLanguageCode;
105+
}
106+
107+
public function setMainLanguageCode(string $mainLanguageCode): void
108+
{
109+
$this->mainLanguageCode = $mainLanguageCode;
110+
}
111+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy;
6+
7+
use Ibexa\Contracts\Core\Repository\Repository;
8+
use Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface;
9+
use Ibexa\Contracts\Taxonomy\Value\TaxonomyEntry;
10+
use Ibexa\Taxonomy\Exception\TaxonomyEntryNotFoundException;
11+
12+
class IbexaTaxonomyImporter
13+
{
14+
protected Repository $repository;
15+
protected TaxonomyServiceInterface $taxonomyService;
16+
protected IbexaTaxonomyCreator $taxonomyCreator;
17+
protected IbexaTaxonomyUpdater $taxonomyUpdater;
18+
19+
public function __construct(
20+
Repository $repository,
21+
TaxonomyServiceInterface $taxonomyService,
22+
IbexaTaxonomyCreator $taxonomyCreator,
23+
IbexaTaxonomyUpdater $taxonomyUpdater,
24+
) {
25+
$this->taxonomyUpdater = $taxonomyUpdater;
26+
$this->taxonomyCreator = $taxonomyCreator;
27+
$this->taxonomyService = $taxonomyService;
28+
$this->repository = $repository;
29+
}
30+
31+
public function __invoke(IbexaTaxonomyData $ibexaTaxonomyData, bool $allowUpdate = true): TaxonomyEntry
32+
{
33+
$remoteId = $ibexaTaxonomyData->getContentRemoteId();
34+
$ownerId = $ibexaTaxonomyData->getOwnerId();
35+
if (null === $ownerId) {
36+
$ownerId = $this->repository
37+
->getPermissionResolver()
38+
->getCurrentUserReference()
39+
->getUserId();
40+
}
41+
42+
try {
43+
$parent = $this->taxonomyService->loadEntryByIdentifier(
44+
$ibexaTaxonomyData->getParentIdentifier(),
45+
$ibexaTaxonomyData->getTaxonomyName()
46+
);
47+
try {
48+
$taxonomyEntry = $this->taxonomyService->loadEntryByIdentifier(
49+
$ibexaTaxonomyData->getIdentifier(),
50+
$ibexaTaxonomyData->getTaxonomyName()
51+
);
52+
if (!$allowUpdate) {
53+
return $taxonomyEntry;
54+
}
55+
56+
return ($this->taxonomyUpdater)(
57+
$taxonomyEntry,
58+
$parent,
59+
$ibexaTaxonomyData->getNames(),
60+
$ownerId,
61+
$ibexaTaxonomyData->getMainLanguageCode()
62+
);
63+
} catch (TaxonomyEntryNotFoundException $exception) {
64+
return ($this->taxonomyCreator)(
65+
$ibexaTaxonomyData->getIdentifier(),
66+
$parent,
67+
$ibexaTaxonomyData->getNames(),
68+
$remoteId,
69+
$ownerId,
70+
$ibexaTaxonomyData->getMainLanguageCode(),
71+
$ibexaTaxonomyData->getSectionId(),
72+
$ibexaTaxonomyData->getModificationDate()
73+
);
74+
}
75+
} catch (\Throwable $exception) {
76+
dump($exception, $ibexaTaxonomyData);
77+
throw $exception;
78+
}
79+
}
80+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Taxonomy;
6+
7+
use AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentUpdater;
8+
use Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface;
9+
use Ibexa\Contracts\Taxonomy\Value\TaxonomyEntry;
10+
use Ibexa\Core\FieldType\TextLine\Value as TextLineValue;
11+
use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
12+
13+
class IbexaTaxonomyUpdater
14+
{
15+
protected TaxonomyServiceInterface $taxonomyService;
16+
protected TaxonomyConfiguration $taxonomyConfiguration;
17+
protected IbexaContentUpdater $contentUpdater;
18+
19+
public function __construct(
20+
TaxonomyServiceInterface $taxonomyService,
21+
TaxonomyConfiguration $taxonomyConfiguration,
22+
IbexaContentUpdater $contentUpdater,
23+
) {
24+
$this->contentUpdater = $contentUpdater;
25+
$this->taxonomyConfiguration = $taxonomyConfiguration;
26+
$this->taxonomyService = $taxonomyService;
27+
}
28+
29+
/**
30+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
31+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException
32+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException
33+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
34+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
35+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
36+
*/
37+
public function __invoke(
38+
TaxonomyEntry $entry,
39+
TaxonomyEntry $parent,
40+
array $names,
41+
int $ownerId = null,
42+
string $mainLanguageCode = 'eng-GB',
43+
bool $hidden = false
44+
): TaxonomyEntry {
45+
if ($entry->parent->identifier !== $parent->identifier) {
46+
$this->taxonomyService->moveEntry($entry, $parent);
47+
}
48+
49+
$fields = [];
50+
foreach ($names as $languageCode => $name) {
51+
$fields[$languageCode] = [
52+
'name' => new TextLineValue($name),
53+
];
54+
}
55+
56+
$content = ($this->contentUpdater)(
57+
$entry->getContent(),
58+
$fields,
59+
[$parent->content->contentInfo->getMainLocation()],
60+
$ownerId,
61+
$mainLanguageCode,
62+
$hidden
63+
);
64+
65+
return $this->taxonomyService->loadEntryByContentId($content->id);
66+
}
67+
}

0 commit comments

Comments
 (0)