|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
4
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor; |
|
7
|
|
|
|
|
8
|
|
|
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException as ApiUnauthorizedException; |
|
9
|
|
|
use eZ\Publish\Core\REST\Common\Output\Generator; |
|
10
|
|
|
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor; |
|
11
|
|
|
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher; |
|
12
|
|
|
use eZ\Publish\Core\REST\Common\Output\Visitor; |
|
13
|
|
|
use eZ\Publish\Core\REST\Server\Output\PathExpansion\ExpansionGenerator; |
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Output\PathExpansion\PathExpansionChecker; |
|
15
|
|
|
use eZ\Publish\Core\REST\Server\Output\PathExpansion\Exceptions\MultipleValueLoadException; |
|
16
|
|
|
use eZ\Publish\Core\REST\Server\Output\PathExpansion\ValueLoaders\UriValueLoader; |
|
17
|
|
|
use eZ\Publish\Core\REST\Server\Values; |
|
18
|
|
|
|
|
19
|
|
|
class ResourceLink extends ValueObjectVisitor |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var PathExpansionChecker |
|
23
|
|
|
*/ |
|
24
|
|
|
private $pathExpansionChecker; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var UriValueLoader |
|
28
|
|
|
*/ |
|
29
|
|
|
private $valueLoader; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ValueObjectVisitorDispatcher |
|
33
|
|
|
*/ |
|
34
|
|
|
private $visitorDispatcher; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
UriValueLoader $valueLoader, |
|
38
|
|
|
PathExpansionChecker $pathExpansionChecker, |
|
39
|
|
|
ValueObjectVisitorDispatcher $visitorDispatcher) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->valueLoader = $valueLoader; |
|
42
|
|
|
$this->pathExpansionChecker = $pathExpansionChecker; |
|
43
|
|
|
$this->visitorDispatcher = $visitorDispatcher; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Visitor $visitor |
|
48
|
|
|
* @param Generator $generator |
|
49
|
|
|
* @param \eZ\Publish\Core\REST\Server\Values\ResourceLink $data |
|
50
|
|
|
*/ |
|
51
|
|
|
public function visit(Visitor $visitor, Generator $generator, $data) |
|
52
|
|
|
{ |
|
53
|
|
|
$generator->startAttribute('href', $data->link); |
|
54
|
|
|
$generator->endAttribute('href'); |
|
55
|
|
|
|
|
56
|
|
|
if ($this->pathExpansionChecker->needsExpansion($generator->getStackPath())) { |
|
57
|
|
|
$response = $visitor->getResponse(); |
|
58
|
|
|
|
|
59
|
|
|
if (!$response->headers->contains('Vary', 'X-eZ-Embed-Value')) { |
|
60
|
|
|
$response->setVary('X-eZ-Embed-Value', false); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$valueObject = $this->valueLoader->load($data->link, $data->mediaType ?: null); |
|
65
|
|
|
if ($valueObject instanceof Values\CachedValue) { |
|
66
|
|
|
$valueObject = $this->processCachedValue($valueObject, $visitor); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->visitorDispatcher->visit( |
|
70
|
|
|
$valueObject, |
|
71
|
|
|
new ExpansionGenerator($generator), |
|
72
|
|
|
$visitor |
|
73
|
|
|
); |
|
74
|
|
|
} catch (ApiUnauthorizedException $e) { |
|
75
|
|
|
$generator->startAttribute('embed-error', $e->getMessage()); |
|
76
|
|
|
$generator->endAttribute('embed-error'); |
|
77
|
|
|
} catch (MultipleValueLoadException $e) { |
|
78
|
|
|
$generator->startAttribute('embed-error', $e->getMessage()); |
|
79
|
|
|
$generator->endAttribute('embed-error'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Adds cache tags from the given $cachedValue, and returns the wrapped value object. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \eZ\Publish\Core\REST\Server\Values\CachedValue $cachedValue |
|
88
|
|
|
* @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor |
|
89
|
|
|
* |
|
90
|
|
|
* @return object The value object wrapped by the $cachedValue |
|
91
|
|
|
*/ |
|
92
|
|
|
private function processCachedValue(Values\CachedValue $cachedValue, Visitor $visitor) |
|
93
|
|
|
{ |
|
94
|
|
View Code Duplication |
if (!empty($cachedValue->cacheTags)) { |
|
|
|
|
|
|
95
|
|
|
$response = $visitor->getResponse(); |
|
96
|
|
|
$tags = []; |
|
97
|
|
|
foreach ($cachedValue->cacheTags as $tag => $values) { |
|
98
|
|
|
foreach ((array)$values as $value) { |
|
99
|
|
|
$tagValue = $tag . '-' . $value; |
|
100
|
|
|
|
|
101
|
|
|
if (!$response->headers->contains('xkey', $tagValue)) { |
|
102
|
|
|
$tags[] = $tagValue; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
$response->headers->set('xkey', $tags, false); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $cachedValue->value; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.