Completed
Push — master ( 553bc5...721b68 )
by
unknown
49:35 queued 26:20
created

Version::visit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Version ValueObjectVisitor class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
10
11
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
12
use eZ\Publish\Core\REST\Common\Output\Generator;
13
use eZ\Publish\Core\REST\Common\Output\Visitor;
14
use eZ\Publish\Core\REST\Common\Output\FieldTypeSerializer;
15
use eZ\Publish\Core\REST\Server\Values\RelationList as RelationListValue;
16
use eZ\Publish\Core\REST\Server\Values\Version as VersionValue;
17
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
18
use eZ\Publish\API\Repository\Values\Content\Field;
19
20
/**
21
 * Version value object visitor.
22
 */
23
class Version extends ValueObjectVisitor
24
{
25
    /**
26
     * @var \eZ\Publish\Core\REST\Common\Output\FieldTypeSerializer
27
     */
28
    protected $fieldTypeSerializer;
29
30
    /**
31
     * @param \eZ\Publish\Core\REST\Common\Output\FieldTypeSerializer $fieldTypeSerializer
32
     */
33
    public function __construct(FieldTypeSerializer $fieldTypeSerializer)
34
    {
35
        $this->fieldTypeSerializer = $fieldTypeSerializer;
36
    }
37
38
    /**
39
     * Visit struct returned by controllers.
40
     *
41
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
42
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
43
     * @param \eZ\Publish\Core\REST\Server\Values\Version $data
44
     */
45
    public function visit(Visitor $visitor, Generator $generator, $data)
46
    {
47
        $generator->startObjectElement('Version');
48
49
        $visitor->setHeader('Content-Type', $generator->getMediaType('Version'));
50
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('VersionUpdate'));
51
        $this->visitVersionAttributes($visitor, $generator, $data);
52
        $generator->endObjectElement('Version');
53
    }
54
55
    /**
56
     * Visits a single content field and generates its content.
57
     *
58
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
59
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
60
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
61
     */
62
    public function visitField(Generator $generator, ContentType $contentType, Field $field)
63
    {
64
        $generator->startHashElement('field');
65
66
        $generator->startValueElement('id', $field->id);
67
        $generator->endValueElement('id');
68
69
        $generator->startValueElement('fieldDefinitionIdentifier', $field->fieldDefIdentifier);
70
        $generator->endValueElement('fieldDefinitionIdentifier');
71
72
        $generator->startValueElement('languageCode', $field->languageCode);
73
        $generator->endValueElement('languageCode');
74
75
        $generator->startValueElement('fieldTypeIdentifier', $field->fieldTypeIdentifier);
76
        $generator->endValueElement('fieldTypeIdentifier');
77
78
        $this->fieldTypeSerializer->serializeFieldValue(
79
            $generator,
80
            $contentType,
81
            $field
82
        );
83
84
        $generator->endHashElement('field');
85
    }
86
87
    protected function visitVersionAttributes(Visitor $visitor, Generator $generator, VersionValue $data)
88
    {
89
        $content = $data->content;
90
91
        $versionInfo = $content->getVersionInfo();
92
        $contentType = $data->contentType;
93
94
        $path = $data->path;
95 View Code Duplication
        if ($path == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
96
            $path = $this->router->generate(
97
                'ezpublish_rest_loadContentInVersion',
98
                array(
99
                    'contentId' => $content->id,
100
                    'versionNumber' => $versionInfo->versionNo,
101
                )
102
            );
103
        }
104
105
        $generator->startAttribute('href', $path);
106
        $generator->endAttribute('href');
107
108
        $visitor->visitValueObject($versionInfo);
109
110
        $generator->startHashElement('Fields');
111
        $generator->startList('field');
112
        foreach ($content->getFields() as $field) {
113
            $this->visitField($generator, $contentType, $field);
114
        }
115
        $generator->endList('field');
116
        $generator->endHashElement('Fields');
117
118
        $visitor->visitValueObject(
119
            new RelationListValue(
120
                $data->relations,
121
                $content->id,
122
                $versionInfo->versionNo
123
            )
124
        );
125
    }
126
}
127