-
Notifications
You must be signed in to change notification settings - Fork 19
/
DimensionContentRepository.php
128 lines (106 loc) · 4.25 KB
/
DimensionContentRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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\Doctrine;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollection;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
class DimensionContentRepository implements DimensionContentRepositoryInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;
public function __construct(
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector
) {
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
}
public function load(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentCollectionInterface {
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));
$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent')
->innerJoin('dimensionContent.' . $mappingProperty, 'content')
->where('content.id = :id')
->setParameter('id', $contentRichEntity->getId());
$effectiveAttributes = $this->getEffectiveAttributes($dimensionContentClass, $dimensionAttributes);
$queryBuilder->addCriteria($this->getAttributesCriteria('dimensionContent', $effectiveAttributes));
$this->addSortBy($queryBuilder, $effectiveAttributes);
/** @var DimensionContentInterface[] $dimensionContents */
$dimensionContents = $queryBuilder->getQuery()->getResult();
return new DimensionContentCollection(
$dimensionContents,
$effectiveAttributes,
$dimensionContentClass
);
}
/**
* Less specific should be returned first to merge correctly.
*
* @param mixed[] $attributes
*/
private function addSortBy(QueryBuilder $queryBuilder, array $attributes): void
{
foreach ($attributes as $key => $value) {
$queryBuilder->addOrderBy('dimensionContent.' . $key);
}
}
/**
* @param mixed[] $attributes
*/
private function getAttributesCriteria(string $alias, array $attributes): Criteria
{
$criteria = Criteria::create();
foreach ($attributes as $key => $value) {
$fieldName = $alias . '.' . $key;
$expr = $criteria->expr()->isNull($fieldName);
if (null !== $value) {
$eqExpr = $criteria->expr()->eq($fieldName, $value);
$expr = $criteria->expr()->orX($expr, $eqExpr);
}
$criteria->andWhere($expr);
}
return $criteria;
}
/**
* @param class-string<DimensionContentInterface> $className
* @param mixed[] $attributes
*
* @return mixed[]
*/
private function getEffectiveAttributes(string $className, array $attributes): array
{
$defaultValues = $className::getDefaultDimensionAttributes();
// Ignore keys that are not part of the default attributes
$attributes = \array_intersect_key($attributes, $defaultValues);
$attributes = \array_merge(
$defaultValues,
$attributes
);
return $attributes;
}
}