Completed
Push — 6.13 ( 055733...f64074 )
by André
23:24 queued 09:44
created

Version::visitField()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 3
dl 0
loc 24
rs 8.9713
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\API\Repository\Values\ContentType\ContentType;
17
use eZ\Publish\API\Repository\Values\Content\Field;
18
19
/**
20
 * Version value object visitor.
21
 */
22
class Version extends ValueObjectVisitor
23
{
24
    /**
25
     * @var \eZ\Publish\Core\REST\Common\Output\FieldTypeSerializer
26
     */
27
    protected $fieldTypeSerializer;
28
29
    /**
30
     * @param \eZ\Publish\Core\REST\Common\Output\FieldTypeSerializer $fieldTypeSerializer
31
     */
32
    public function __construct(FieldTypeSerializer $fieldTypeSerializer)
33
    {
34
        $this->fieldTypeSerializer = $fieldTypeSerializer;
35
    }
36
37
    /**
38
     * Visit struct returned by controllers.
39
     *
40
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
41
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
42
     * @param \eZ\Publish\Core\REST\Server\Values\Version $data
43
     */
44
    public function visit(Visitor $visitor, Generator $generator, $data)
45
    {
46
        $content = $data->content;
47
48
        $versionInfo = $content->getVersionInfo();
49
        $contentType = $data->contentType;
50
51
        $generator->startObjectElement('Version');
52
53
        $visitor->setHeader('Content-Type', $generator->getMediaType('Version'));
54
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('VersionUpdate'));
55
56
        $path = $data->path;
57 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...
58
            $path = $this->router->generate(
59
                'ezpublish_rest_loadContentInVersion',
60
                array(
61
                    'contentId' => $content->id,
62
                    'versionNumber' => $versionInfo->versionNo,
63
                )
64
            );
65
        }
66
67
        $generator->startAttribute('href', $path);
68
        $generator->endAttribute('href');
69
70
        $visitor->visitValueObject($versionInfo);
71
72
        $generator->startHashElement('Fields');
73
        $generator->startList('field');
74
        foreach ($content->getFields() as $field) {
75
            $this->visitField($generator, $contentType, $field);
76
        }
77
        $generator->endList('field');
78
        $generator->endHashElement('Fields');
79
80
        $visitor->visitValueObject(
81
            new RelationListValue(
82
                $data->relations,
83
                $content->id,
84
                $versionInfo->versionNo
85
            )
86
        );
87
88
        $generator->endObjectElement('Version');
89
    }
90
91
    /**
92
     * Visits a single content field and generates its content.
93
     *
94
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
95
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
96
     * @param \eZ\Publish\API\Repository\Values\Content\Field $field
97
     */
98
    public function visitField(Generator $generator, ContentType $contentType, Field $field)
99
    {
100
        $generator->startHashElement('field');
101
102
        $generator->startValueElement('id', $field->id);
103
        $generator->endValueElement('id');
104
105
        $generator->startValueElement('fieldDefinitionIdentifier', $field->fieldDefIdentifier);
106
        $generator->endValueElement('fieldDefinitionIdentifier');
107
108
        $generator->startValueElement('languageCode', $field->languageCode);
109
        $generator->endValueElement('languageCode');
110
111
        $generator->startValueElement('typeIdentifier', $field->typeIdentifier);
112
        $generator->endValueElement('typeIdentifier');
113
114
        $this->fieldTypeSerializer->serializeFieldValue(
115
            $generator,
116
            $contentType,
117
            $field
118
        );
119
120
        $generator->endHashElement('field');
121
    }
122
}
123