-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPagerLocationToDataMapper.php
More file actions
121 lines (107 loc) · 3.98 KB
/
PagerLocationToDataMapper.php
File metadata and controls
121 lines (107 loc) · 3.98 KB
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
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\AdminUi\Tab\Dashboard;
use Ibexa\Contracts\Core\Repository\Exceptions\BadStateException;
use Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\LanguageService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\Language;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Core\Repository\LocationResolver\LocationResolver;
use Pagerfanta\Pagerfanta;
final class PagerLocationToDataMapper
{
/** @var UserService */
private $userService;
/** @var LocationResolver */
private $locationResolver;
private LanguageService $languageService;
public function __construct(
UserService $userService,
LocationResolver $locationResolver,
LanguageService $languageService
) {
$this->userService = $userService;
$this->locationResolver = $locationResolver;
$this->languageService = $languageService;
}
/**
* @param Pagerfanta<Location> $pager
*
* @return array<
* array{
* 'contentTypeId': int,
* 'contentId': int,
* 'name': string,
* 'type': ?string,
* 'language': string,
* 'available_enabled_translations': Language[],
* 'contributor': ?User,
* 'content_type': ContentType,
* 'modified': \DateTime,
* 'resolvedLocation': Location
* }
* >
*
* @throws ForbiddenException
* @throws BadStateException
* @throws NotFoundException
*/
public function map(
Pagerfanta $pager,
bool $doMapVersionInfoData = false
): array {
$data = [];
/** @var Location $location */
foreach ($pager as $location) {
$contentInfo = $location->getContentInfo();
$versionInfo = $doMapVersionInfoData ? $location->getContent()->getVersionInfo() : null;
$contentType = $location->getContentInfo()->getContentType();
$data[] = [
'contentTypeId' => $contentInfo->contentTypeId,
'contentId' => $contentInfo->id,
'name' => $contentInfo->name,
'type' => $contentType->getName(),
'language' => $contentInfo->mainLanguageCode,
'available_enabled_translations' => $versionInfo !== null ? $this->getAvailableTranslations($versionInfo) : [],
'contributor' => $versionInfo !== null ? $this->getVersionContributor($versionInfo) : null,
'content_type' => $contentType,
'modified' => $contentInfo->modificationDate,
'resolvedLocation' => $this->locationResolver->resolveLocation($contentInfo),
];
}
return $data;
}
private function getVersionContributor(VersionInfo $versionInfo): ?User
{
try {
return $this->userService->loadUser($versionInfo->creatorId);
} catch (NotFoundException $e) {
return null;
}
}
/**
* @return Language[]
*/
private function getAvailableTranslations(
VersionInfo $versionInfo
): array {
$availableTranslationsLanguages = $this->languageService->loadLanguageListByCode(
$versionInfo->languageCodes
);
return array_filter(
$availableTranslationsLanguages,
static function (Language $language): bool {
return $language->enabled;
}
);
}
}