-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathElementProviderTrait.php
More file actions
135 lines (119 loc) · 3.98 KB
/
ElementProviderTrait.php
File metadata and controls
135 lines (119 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\StudioBackendBundle\Util\Trait;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Document;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
use Pimcore\Model\Version;
use function get_class;
/**
* @internal
*/
trait ElementProviderTrait
{
/**
* @throws NotFoundException
*/
private function getElement(
ServiceResolverInterface $serviceResolver,
string $type,
int $id
): ElementInterface {
$element = $serviceResolver->getElementById($this->getCoreElementType($type), $id);
if ($element === null) {
throw new NotFoundException($type, $id);
}
return $element;
}
/**
* @throws NotFoundException
*/
private function getElementByPath(
ServiceResolverInterface $serviceResolver,
string $type,
string $path
): ElementInterface {
$element = $serviceResolver->getElementByPath($this->getCoreElementType($type), $path);
if ($element === null) {
throw new NotFoundException($type, $path, 'path');
}
return $element;
}
private function getLatestVersionForUser(
ElementInterface $element,
?UserInterface $user,
): ?Version {
if (!$element instanceof Asset &&
!$element instanceof Document\PageSnippet &&
!$element instanceof Concrete
) {
return null;
}
// check for latest version
return $element->getLatestVersion($user?->getId());
}
private function getVersionData(ElementInterface $element, ?Version $version): ElementInterface
{
if (!$version) {
return $element;
}
return $version->getData();
}
/**
* @throws InvalidElementTypeException
*/
private function getElementClass(ElementInterface $element): string
{
return match (true) {
$element instanceof Asset => Asset::class,
$element instanceof Document => Document::class,
$element instanceof DataObject => DataObject::class,
default => throw new InvalidElementTypeException(get_class($element))
};
}
/**
* @throws InvalidElementTypeException
*/
private function getElementType(ElementInterface $element, bool $getCoreType = false): string
{
return match (true) {
$element instanceof Asset => ElementTypes::TYPE_ASSET,
$element instanceof Document => ElementTypes::TYPE_DOCUMENT,
$element instanceof DataObject =>
$getCoreType ? ElementTypes::TYPE_OBJECT : ElementTypes::TYPE_DATA_OBJECT,
default => throw new InvalidElementTypeException($element->getType())
};
}
private function getCoreElementType(string $type): string
{
if ($type === ElementTypes::TYPE_DATA_OBJECT) {
return ElementTypes::TYPE_OBJECT;
}
return $type;
}
private function getElementFullPath(
string $parentFullPath,
string $key
): string {
return str_ends_with($parentFullPath, '/') === true ? $parentFullPath . $key : $parentFullPath . '/' . $key;
}
}