Completed
Push — master ( 745b87...dded74 )
by André
13:55
created

RelationConverter::toStorageFieldDefinition()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 35
Code Lines 25

Duplication

Lines 8
Ratio 22.86 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 8
nop 2
dl 8
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Relation converter.
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\Persistence\Legacy\Content\FieldValue\Converter;
10
11
use DOMDocument;
12
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
13
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14
use eZ\Publish\SPI\Persistence\Content\FieldValue;
15
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
16
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
17
18
class RelationConverter implements Converter
19
{
20
    /**
21
     * Factory for current class.
22
     *
23
     * Note: Class should instead be configured as service if it gains dependencies.
24
     *
25
     * @deprecated since 6.8, will be removed in 7.x, use default constructor instead.
26
     *
27
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter
28
     */
29
    public static function create()
30
    {
31
        return new self();
32
    }
33
34
    /**
35
     * Converts data from $value to $storageFieldValue.
36
     *
37
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
38
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
39
     */
40 View Code Duplication
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
41
    {
42
        $storageFieldValue->dataInt = !empty($value->data['destinationContentId'])
43
            ? $value->data['destinationContentId']
44
            : null;
45
        $storageFieldValue->sortKeyInt = (int)$value->sortKey;
46
    }
47
48
    /**
49
     * Converts data from $value to $fieldValue.
50
     *
51
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
52
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
53
     */
54 View Code Duplication
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
55
    {
56
        $fieldValue->data = [
57
            'destinationContentId' => $value->dataInt ?: null,
58
        ];
59
        $fieldValue->sortKey = (int)$value->sortKeyInt;
60
    }
61
62
    /**
63
     * Converts field definition data in $fieldDef into $storageFieldDef.
64
     *
65
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
66
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
67
     */
68
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
69
    {
70
        $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
71
        $doc = new DOMDocument('1.0', 'utf-8');
72
        $root = $doc->createElement('related-objects');
73
        $doc->appendChild($root);
74
75
        $constraints = $doc->createElement('constraints');
76 View Code Duplication
        if (!empty($fieldSettings['selectionContentTypes'])) {
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...
77
            foreach ($fieldSettings['selectionContentTypes'] as $typeIdentifier) {
78
                $allowedClass = $doc->createElement('allowed-class');
79
                $allowedClass->setAttribute('contentclass-identifier', $typeIdentifier);
80
                $constraints->appendChild($allowedClass);
81
                unset($allowedClass);
82
            }
83
        }
84
        $root->appendChild($constraints);
85
86
        $selectionType = $doc->createElement('selection_type');
87
        if (isset($fieldSettings['selectionMethod'])) {
88
            $selectionType->setAttribute('value', (int)$fieldSettings['selectionMethod']);
89
        } else {
90
            $selectionType->setAttribute('value', 0);
91
        }
92
        $root->appendChild($selectionType);
93
94
        $defaultLocation = $doc->createElement('contentobject-placement');
95
        if (!empty($fieldSettings['selectionRoot'])) {
96
            $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionRoot']);
97
        }
98
        $root->appendChild($defaultLocation);
99
100
        $doc->appendChild($root);
101
        $storageDef->dataText5 = $doc->saveXML();
102
    }
103
104
    /**
105
     * Converts field definition data in $storageDef into $fieldDef.
106
     *
107
     * <code>
108
     *   <?xml version="1.0" encoding="utf-8"?>
109
     *   <related-objects>
110
     *     <constraints>
111
     *       <allowed-class contentclass-identifier="blog_post"/>
112
     *     </constraints>
113
     *     <selection_type value="1"/>
114
     *     <contentobject-placement node-id="67"/>
115
     *   </related-objects>
116
     *
117
     *   <?xml version="1.0" encoding="utf-8"?>
118
     *   <related-objects>
119
     *     <constraints/>
120
     *     <selection_type value="0"/>
121
     *     <contentobject-placement/>
122
     *   </related-objects>
123
     * </code>
124
     *
125
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
126
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
127
     */
128
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
129
    {
130
        // default settings
131
        // use dataInt1 and dataInt2 fields as default for backward compatibility
132
        $fieldDef->fieldTypeConstraints->fieldSettings = [
133
            'selectionMethod' => $storageDef->dataInt1,
134
            'selectionRoot' => $storageDef->dataInt2 === 0 ? '' : $storageDef->dataInt2,
135
            'selectionContentTypes' => [],
136
        ];
137
138
        if ($storageDef->dataText5 === null) {
139
            return;
140
        }
141
142
        // read settings from storage
143
        $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings;
144
        $dom = new DOMDocument('1.0', 'utf-8');
145
        if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) {
146
            return;
147
        }
148
149
        if ($selectionType = $dom->getElementsByTagName('selection_type')) {
150
            $fieldSettings['selectionMethod'] = (int)$selectionType->item(0)->getAttribute('value');
151
        }
152
153 View Code Duplication
        if (
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...
154
            ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')) &&
155
            $defaultLocation->item(0)->hasAttribute('node-id')
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on DOMNode. Did you maybe mean hasAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
156
        ) {
157
            $fieldSettings['selectionRoot'] = (int)$defaultLocation->item(0)->getAttribute('node-id');
158
        }
159
160
        if (!($constraints = $dom->getElementsByTagName('constraints'))) {
161
            return;
162
        }
163
164
        foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) {
165
            $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier');
166
        }
167
    }
168
169
    /**
170
     * Returns the name of the index column in the attribute table.
171
     *
172
     * Returns the name of the index column the datatype uses, which is either
173
     * "sort_key_int" or "sort_key_string". This column is then used for
174
     * filtering and sorting for this type.
175
     *
176
     * @return string
177
     */
178
    public function getIndexColumn()
179
    {
180
        return 'sort_key_int';
181
    }
182
}
183