-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Florian ALEXANDRE
committed
Aug 1, 2024
1 parent
5c05846
commit 9e8760a
Showing
12 changed files
with
328 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
components/ImportExportBundle/src/lib/Writer/Ibexa/Content/AbstractIbexaContentHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content; | ||
|
||
use Ibexa\Contracts\Core\Repository\Repository; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\ContentStruct; | ||
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; | ||
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; | ||
|
||
class AbstractIbexaContentHandler | ||
{ | ||
protected Repository $repository; | ||
|
||
public function __construct( | ||
Repository $repository | ||
) { | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $fieldsByLanguages | ||
*/ | ||
protected function setContentFields( | ||
ContentType $contentType, | ||
ContentStruct $contentStruct, | ||
array $fieldsByLanguages | ||
): void { | ||
foreach ($fieldsByLanguages as $languageCode => $fields) { | ||
foreach ($fields as $fieldID => $field) { | ||
$fieldDefinition = $contentType->getFieldDefinition($fieldID); | ||
if ($fieldDefinition instanceof FieldDefinition) { | ||
$contentStruct->setField($fieldID, $field, $languageCode); | ||
} | ||
} | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentCreator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content; | ||
|
||
use DateTime; | ||
use Exception; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Content; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Location; | ||
|
||
class IbexaContentCreator extends AbstractIbexaContentHandler | ||
{ | ||
/** | ||
* @param array<string|int, int|string|Location> $parentLocationIdList | ||
* @param array<string, mixed> $fieldsByLanguages | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
*/ | ||
public function __invoke( | ||
string $contentTypeIdentifier, | ||
array $parentLocationIdList, | ||
array $fieldsByLanguages, | ||
string $remoteId, | ||
int $ownerId = null, | ||
string $languageCode = 'eng-GB', | ||
int $sectionId = null, | ||
$modificationDate = null, | ||
bool $hidden = false | ||
): Content { | ||
$contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier( | ||
$contentTypeIdentifier | ||
); | ||
|
||
/* Creating new content create structure */ | ||
$contentCreateStruct = $this->repository->getContentService()->newContentCreateStruct( | ||
$contentType, | ||
$languageCode | ||
); | ||
$contentCreateStruct->remoteId = $remoteId; | ||
$contentCreateStruct->ownerId = $ownerId; | ||
if (null !== $modificationDate) { | ||
$contentCreateStruct->modificationDate = $modificationDate instanceof DateTime ? | ||
$modificationDate : | ||
DateTime::createFromFormat('U', (string) $modificationDate); | ||
} | ||
|
||
if ($sectionId) { | ||
$contentCreateStruct->sectionId = $sectionId; | ||
} | ||
|
||
/* Update content structure fields */ | ||
$this->setContentFields($contentType, $contentCreateStruct, $fieldsByLanguages); | ||
|
||
/* Assigning the content locations */ | ||
$locationCreateStructs = []; | ||
foreach ($parentLocationIdList as $locationRemoteId => $parentLocationId) { | ||
if (empty($parentLocationId)) { | ||
throw new Exception('Parent location id cannot be empty'); | ||
} | ||
if ($parentLocationId instanceof Location) { | ||
$parentLocationId = $parentLocationId->id; | ||
} | ||
if (is_string($parentLocationId)) { | ||
$parentLocationId = $this->repository->getLocationService()->loadLocationByRemoteId( | ||
$parentLocationId | ||
)->id; | ||
} | ||
$locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct( | ||
$parentLocationId | ||
); | ||
if (is_string($locationRemoteId)) { | ||
$locationCreateStruct->remoteId = $locationRemoteId; | ||
} | ||
if ($hidden) { | ||
$locationCreateStruct->hidden = true; | ||
} | ||
$locationCreateStructs[] = $locationCreateStruct; | ||
} | ||
|
||
/* Creating new draft */ | ||
$draft = $this->repository->getContentService()->createContent( | ||
$contentCreateStruct, | ||
$locationCreateStructs | ||
); | ||
|
||
/* Publish the new content draft */ | ||
return $this->repository->getContentService()->publishVersion($draft->versionInfo); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentImporter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content; | ||
|
||
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; | ||
use Ibexa\Contracts\Core\Repository\Repository; | ||
|
||
class IbexaContentImporter | ||
{ | ||
protected Repository $repository; | ||
protected IbexaContentUpdater $contentUpdater; | ||
protected IbexaContentCreator $contentCreator; | ||
|
||
public function __construct( | ||
Repository $repository, | ||
IbexaContentUpdater $contentUpdater, | ||
IbexaContentCreator $contentCreator | ||
) { | ||
$this->contentCreator = $contentCreator; | ||
$this->contentUpdater = $contentUpdater; | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* @param \AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentData $contentData | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
* | ||
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Content | ||
*/ | ||
public function __invoke(IbexaContentData $contentData, bool $allowUpdate = true) | ||
{ | ||
$remoteId = $contentData->getContentRemoteId(); | ||
$ownerId = $contentData->getOwnerId(); | ||
if (null === $ownerId) { | ||
$ownerId = $this->repository | ||
->getPermissionResolver() | ||
->getCurrentUserReference() | ||
->getUserId(); | ||
} | ||
|
||
try { | ||
try { | ||
$content = $this->repository->getContentService()->loadContentByRemoteId( | ||
$contentData->getContentRemoteId() | ||
); | ||
if (!$allowUpdate) { | ||
return $content; | ||
} | ||
|
||
return ($this->contentUpdater)( | ||
$content, | ||
$contentData->getFields(), | ||
$ownerId, | ||
$contentData->getMainLanguageCode() | ||
); | ||
} catch (NotFoundException $exception) { | ||
return ($this->contentCreator)( | ||
$contentData->getContentTypeIdentifier(), | ||
$contentData->getParentLocationIdList(), | ||
$contentData->getFields(), | ||
$remoteId, | ||
$ownerId, | ||
$contentData->getMainLanguageCode(), | ||
$contentData->getSectionId(), | ||
$contentData->getModificationDate() | ||
); | ||
} | ||
} catch (\Throwable $exception) { | ||
dump($exception); | ||
throw $exception; | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentUpdater.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Content; | ||
|
||
class IbexaContentUpdater extends AbstractIbexaContentHandler | ||
{ | ||
/** | ||
* @param array<string, mixed> $fieldsByLanguages | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
*/ | ||
public function __invoke( | ||
Content $content, | ||
array $fieldsByLanguages, | ||
int $ownerId = null, | ||
string $mainLanguageCode = 'eng-GB' | ||
): Content { | ||
$contentType = $this->repository->getContentTypeService()->loadContentType( | ||
$content->contentInfo->contentTypeId | ||
); | ||
|
||
$contentInfo = $content->contentInfo; | ||
$contentDraft = $this->repository->getContentService()->createContentDraft($contentInfo); | ||
|
||
/* Creating new content update structure */ | ||
$contentUpdateStruct = $this->repository | ||
->getContentService() | ||
->newContentUpdateStruct(); | ||
$contentUpdateStruct->initialLanguageCode = $mainLanguageCode; // set language for new version | ||
$contentUpdateStruct->creatorId = $ownerId; | ||
|
||
$this->setContentFields( | ||
$contentType, | ||
$contentUpdateStruct, | ||
$fieldsByLanguages, | ||
); | ||
|
||
$contentDraft = $this->repository->getContentService()->updateContent( | ||
$contentDraft->versionInfo, | ||
$contentUpdateStruct | ||
); | ||
|
||
/* Publish the new content draft */ | ||
return $this->repository->getContentService()->publishVersion($contentDraft->versionInfo); | ||
} | ||
} |
Oops, something went wrong.