Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

ResourceLink::visit()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 3
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
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
18
class ResourceLink extends ValueObjectVisitor
19
{
20
    /**
21
     * @var PathExpansionChecker
22
     */
23
    private $pathExpansionChecker;
24
25
    /**
26
     * @var UriValueLoader
27
     */
28
    private $valueLoader;
29
30
    /**
31
     * @var ValueObjectVisitorDispatcher
32
     */
33
    private $visitorDispatcher;
34
35
    public function __construct(
36
        UriValueLoader $valueLoader,
37
        PathExpansionChecker $pathExpansionChecker,
38
        ValueObjectVisitorDispatcher $visitorDispatcher)
39
    {
40
        $this->valueLoader = $valueLoader;
41
        $this->pathExpansionChecker = $pathExpansionChecker;
42
        $this->visitorDispatcher = $visitorDispatcher;
43
    }
44
45
    /**
46
     * @param Visitor $visitor
47
     * @param Generator $generator
48
     * @param \eZ\Publish\Core\REST\Server\Values\ResourceLink $data
49
     */
50
    public function visit(Visitor $visitor, Generator $generator, $data)
51
    {
52
        $generator->startAttribute('href', $data->link);
53
        $generator->endAttribute('href');
54
55
        if ($this->pathExpansionChecker->needsExpansion($generator->getStackPath())) {
56
            $visitor->addVary('X-eZ-Embed-Value');
57
            try {
58
                $this->visitorDispatcher->visit(
59
                    $this->valueLoader->load($data->link, $data->mediaType ?: null),
60
                    new ExpansionGenerator($generator),
61
                    $visitor
62
                );
63
            } catch (ApiUnauthorizedException $e) {
64
                $generator->startAttribute('embed-error', $e->getMessage());
65
                $generator->endAttribute('embed-error');
66
            } catch (MultipleValueLoadException $e) {
67
                $generator->startAttribute('embed-error', $e->getMessage());
68
                $generator->endAttribute('embed-error');
69
            }
70
        }
71
    }
72
}
73