Completed
Push — ezp26297-rest_embedding_http_c... ( 86d202 )
by
unknown
55:16 queued 26:18
created

ResourceLink::visit()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 4
nop 3
dl 0
loc 21
rs 8.7624
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
            try {
57
                $this->visitorDispatcher->visit(
58
                    $this->valueLoader->load($data->link, $data->mediaType ?: null),
59
                    new ExpansionGenerator($generator),
60
                    $visitor
61
                );
62
            } catch (ApiUnauthorizedException $e) {
63
                $generator->startAttribute('embed-error', $e->getMessage());
64
                $generator->endAttribute('embed-error');
65
            } catch (MultipleValueLoadException $e) {
66
                $generator->startAttribute('embed-error', $e->getMessage());
67
                $generator->endAttribute('embed-error');
68
            }
69
        }
70
    }
71
}
72