-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ContentLinkProvider * Add tests * Fix namespace * Trigger ci
- Loading branch information
Showing
10 changed files
with
552 additions
and
88 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
Content/Infrastructure/Sulu/Link/ContentLinkProvider.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,128 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Link; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Traits\FindContentRichEntitiesTrait; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Traits\ResolveContentDimensionUrlTrait; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Traits\ResolveContentTrait; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface; | ||
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; | ||
|
||
abstract class ContentLinkProvider implements LinkProviderInterface | ||
{ | ||
use FindContentRichEntitiesTrait; | ||
use ResolveContentDimensionUrlTrait; | ||
use ResolveContentTrait; | ||
|
||
/** | ||
* @var StructureMetadataFactoryInterface | ||
*/ | ||
protected $structureMetadataFactory; | ||
|
||
/** | ||
* @var ContentManagerInterface | ||
*/ | ||
protected $contentManager; | ||
|
||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
protected $entityManager; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $contentRichEntityClass; | ||
|
||
public function __construct( | ||
ContentManagerInterface $contentManager, | ||
StructureMetadataFactoryInterface $structureMetadataFactory, | ||
EntityManagerInterface $entityManager, | ||
string $contentRichEntityClass | ||
) { | ||
$this->contentManager = $contentManager; | ||
$this->structureMetadataFactory = $structureMetadataFactory; | ||
$this->entityManager = $entityManager; | ||
$this->contentRichEntityClass = $contentRichEntityClass; | ||
} | ||
|
||
public function preload(array $hrefs, $locale, $published = true): array | ||
{ | ||
if (0 === \count($hrefs)) { | ||
return []; | ||
} | ||
$items = $this->findEntitiesByIds($hrefs); | ||
|
||
return | ||
\array_values( | ||
\array_filter( | ||
\array_map(function(ContentRichEntityInterface $contentRichEntity) use ($locale, $published) { | ||
/** @var DimensionContentInterface|null $resolvedDimensionContent */ | ||
$resolvedDimensionContent = $this->resolveContent($contentRichEntity, $locale, !$published); | ||
|
||
if (!$resolvedDimensionContent) { | ||
return null; | ||
} | ||
|
||
$data = $this->contentManager->normalize($resolvedDimensionContent); | ||
|
||
return new LinkItem( | ||
$contentRichEntity->getId(), | ||
(string) $this->getTitle($resolvedDimensionContent, $data), | ||
(string) $this->getUrl($resolvedDimensionContent, $data), | ||
$published | ||
); | ||
}, $items) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed[] $data | ||
*/ | ||
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string | ||
{ | ||
return $data['title'] ?? $data['name'] ?? null; | ||
} | ||
|
||
protected function getEntityIdField(): string | ||
{ | ||
return 'id'; | ||
} | ||
|
||
protected function getContentRichEntityClass(): string | ||
{ | ||
return $this->contentRichEntityClass; | ||
} | ||
|
||
protected function getStructureMetadataFactory(): StructureMetadataFactoryInterface | ||
{ | ||
return $this->structureMetadataFactory; | ||
} | ||
|
||
protected function getEntityManager(): EntityManagerInterface | ||
{ | ||
return $this->entityManager; | ||
} | ||
|
||
protected function getContentManager(): ContentManagerInterface | ||
{ | ||
return $this->contentManager; | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
Content/Infrastructure/Sulu/Traits/FindContentRichEntitiesTrait.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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Traits; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\ContentWorkflowInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; | ||
|
||
trait FindContentRichEntitiesTrait | ||
{ | ||
/** | ||
* @param string[]|int[] $ids | ||
* | ||
* @return ContentRichEntityInterface[] | ||
*/ | ||
protected function findEntitiesByIds(array $ids): array | ||
{ | ||
$entityIdField = $this->getEntityIdField(); | ||
$entityManager = $this->getEntityManager(); | ||
$contentRichEntityClass = $this->getContentRichEntityClass(); | ||
$classMetadata = $entityManager->getClassMetadata($contentRichEntityClass); | ||
|
||
$entities = $entityManager->createQueryBuilder() | ||
->select(ContentWorkflowInterface::CONTENT_RICH_ENTITY_CONTEXT_KEY) | ||
->from($contentRichEntityClass, ContentWorkflowInterface::CONTENT_RICH_ENTITY_CONTEXT_KEY) | ||
->where(ContentWorkflowInterface::CONTENT_RICH_ENTITY_CONTEXT_KEY . '.' . $entityIdField . ' IN (:ids)') | ||
->getQuery() | ||
->setParameter('ids', $ids) | ||
->getResult(); | ||
|
||
$idPositions = \array_flip($ids); | ||
|
||
\usort( | ||
$entities, | ||
function(ContentRichEntityInterface $a, ContentRichEntityInterface $b) use ($idPositions, $classMetadata, $entityIdField) { | ||
$aId = $classMetadata->getIdentifierValues($a)[$entityIdField]; | ||
$bId = $classMetadata->getIdentifierValues($b)[$entityIdField]; | ||
|
||
return $idPositions[$aId] - $idPositions[$bId]; | ||
} | ||
); | ||
|
||
return $entities; | ||
} | ||
|
||
abstract protected function getEntityIdField(): string; | ||
|
||
abstract protected function getContentRichEntityClass(): string; | ||
|
||
abstract protected function getEntityManager(): EntityManagerInterface; | ||
} |
52 changes: 52 additions & 0 deletions
52
Content/Infrastructure/Sulu/Traits/ResolveContentDimensionUrlTrait.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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Traits; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; | ||
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; | ||
|
||
trait ResolveContentDimensionUrlTrait | ||
{ | ||
/** | ||
* @param mixed[] $data | ||
*/ | ||
protected function getUrl(DimensionContentInterface $dimensionContent, array $data): ?string | ||
{ | ||
if (!$dimensionContent instanceof TemplateInterface) { | ||
// TODO FIXME add testcase for it | ||
return null; // @codeCoverageIgnore | ||
} | ||
|
||
$type = $dimensionContent::getTemplateType(); | ||
$template = $dimensionContent->getTemplateKey(); | ||
|
||
$metadata = $this->getStructureMetadataFactory()->getStructureMetadata($type, $template); | ||
|
||
if (!$metadata) { | ||
// TODO FIXME add testcase for it | ||
return null; // @codeCoverageIgnore | ||
} | ||
|
||
foreach ($metadata->getProperties() as $property) { | ||
if ('route' === $property->getType()) { | ||
return $dimensionContent->getTemplateData()[$property->getName()] ?? null; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
abstract protected function getStructureMetadataFactory(): StructureMetadataFactoryInterface; | ||
} |
Oops, something went wrong.