-
Notifications
You must be signed in to change notification settings - Fork 19
/
DimensionContentCollectionFactory.php
125 lines (104 loc) · 4.63 KB
/
DimensionContentCollectionFactory.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
<?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\Application\DimensionContentCollectionFactory;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\ContentDataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\DimensionContentCollectionFactoryInterface;
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;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
class DimensionContentCollectionFactory implements DimensionContentCollectionFactoryInterface
{
/**
* @var DimensionContentRepositoryInterface
*/
private $dimensionContentRepository;
/**
* @var ContentDataMapperInterface
*/
private $contentDataMapper;
/**
* @var PropertyAccessorInterface
*/
private $propertyAccessor;
public function __construct(
DimensionContentRepositoryInterface $dimensionContentRepository,
ContentDataMapperInterface $contentDataMapper,
PropertyAccessor $propertyAccessor
) {
$this->dimensionContentRepository = $dimensionContentRepository;
$this->contentDataMapper = $contentDataMapper;
$this->propertyAccessor = $propertyAccessor;
}
public function create(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes,
array $data
): DimensionContentCollectionInterface {
$dimensionContentCollection = $this->dimensionContentRepository->load($contentRichEntity, $dimensionAttributes);
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$orderedContentDimensions = iterator_to_array($dimensionContentCollection);
$dimensionContents = new ArrayCollection($orderedContentDimensions);
$unlocalizedAttributes = $dimensionAttributes;
$unlocalizedAttributes['locale'] = null;
// get or create unlocalized dimension content
$unlocalizedDimensionContent = $dimensionContentCollection->getDimensionContent($unlocalizedAttributes);
if (!$unlocalizedDimensionContent) {
$unlocalizedDimensionContent = $this->createContentDimension(
$contentRichEntity,
$dimensionContents,
$unlocalizedAttributes
);
$orderedContentDimensions[] = $unlocalizedDimensionContent;
}
$localizedDimensionContent = null;
if (isset($dimensionAttributes['locale'])) {
// get or create localized dimension content
$localizedDimensionContent = $dimensionContentCollection->getDimensionContent($dimensionAttributes);
if (!$localizedDimensionContent) {
$localizedDimensionContent = $this->createContentDimension(
$contentRichEntity,
$dimensionContents,
$dimensionAttributes
);
$orderedContentDimensions[] = $localizedDimensionContent;
}
}
$this->contentDataMapper->map($data, $unlocalizedDimensionContent, $localizedDimensionContent);
return new DimensionContentCollection(
$orderedContentDimensions,
$dimensionAttributes,
$dimensionContentCollection->getDimensionContentClass()
);
}
/**
* @param Collection<int, DimensionContentInterface> $dimensionContents
* @param mixed[] $attributes
*/
private function createContentDimension(
ContentRichEntityInterface $contentRichEntity,
Collection $dimensionContents,
array $attributes
): DimensionContentInterface {
$dimensionContent = $contentRichEntity->createDimensionContent();
foreach ($attributes as $attributeName => $attributeValue) {
$this->propertyAccessor->setValue($dimensionContent, $attributeName, $attributeValue);
}
$contentRichEntity->addDimensionContent($dimensionContent);
$dimensionContents->add($dimensionContent);
return $dimensionContent;
}
}