Completed
Push — master ( dd6203...33f862 )
by André
22:54 queued 09:02
created

RelationConverter::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
        $selectionMethod = isset($fieldSettings['selectionMethod']) ? (int)$fieldSettings['selectionMethod'] : 0;
88
        $selectionType->setAttribute('value', $selectionMethod);
89
        $root->appendChild($selectionType);
90
91
        $defaultLocation = $doc->createElement('contentobject-placement');
92
        if (!empty($fieldSettings['selectionRoot'])) {
93
            $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionRoot']);
94
        }
95
        $root->appendChild($defaultLocation);
96
97
        $doc->appendChild($root);
98
        $storageDef->dataText5 = $doc->saveXML();
99
100
        // BC: For Backwards Compatibility for legacy and in case of downgrades or data sharing
101
        // Selection method, 0 = browse, 1 = dropdown
102
        $storageDef->dataInt1 = $selectionMethod;
103
104
        // Selection root, location ID, or 0 if empty
105
        $storageDef->dataInt2 = (int)$fieldSettings['selectionRoot'];
106
    }
107
108
    /**
109
     * Converts field definition data in $storageDef into $fieldDef.
110
     *
111
     * <code>
112
     *   <?xml version="1.0" encoding="utf-8"?>
113
     *   <related-objects>
114
     *     <constraints>
115
     *       <allowed-class contentclass-identifier="blog_post"/>
116
     *     </constraints>
117
     *     <selection_type value="1"/>
118
     *     <contentobject-placement node-id="67"/>
119
     *   </related-objects>
120
     *
121
     *   <?xml version="1.0" encoding="utf-8"?>
122
     *   <related-objects>
123
     *     <constraints/>
124
     *     <selection_type value="0"/>
125
     *     <contentobject-placement/>
126
     *   </related-objects>
127
     * </code>
128
     *
129
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
130
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
131
     */
132
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
133
    {
134
        // default settings
135
        // use dataInt1 and dataInt2 fields as default for backward compatibility
136
        $fieldDef->fieldTypeConstraints->fieldSettings = [
137
            'selectionMethod' => $storageDef->dataInt1,
138
            'selectionRoot' => $storageDef->dataInt2 === 0 ? '' : $storageDef->dataInt2,
139
            'selectionContentTypes' => [],
140
        ];
141
142
        if ($storageDef->dataText5 === null) {
143
            return;
144
        }
145
146
        // read settings from storage
147
        $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings;
148
        $dom = new DOMDocument('1.0', 'utf-8');
149
        if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) {
150
            return;
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
            ($selectionType = $dom->getElementsByTagName('selection_type')->item(0)) &&
155
            $selectionType->hasAttribute('value')
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['selectionMethod'] = (int)$selectionType->getAttribute('value');
158
        }
159
160 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...
161
            ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')->item(0)) &&
162
            $defaultLocation->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...
163
        ) {
164
            $fieldSettings['selectionRoot'] = (int)$defaultLocation->getAttribute('node-id');
165
        }
166
167
        if (!($constraints = $dom->getElementsByTagName('constraints'))) {
168
            return;
169
        }
170
171
        foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) {
172
            $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier');
173
        }
174
    }
175
176
    /**
177
     * Returns the name of the index column in the attribute table.
178
     *
179
     * Returns the name of the index column the datatype uses, which is either
180
     * "sort_key_int" or "sort_key_string". This column is then used for
181
     * filtering and sorting for this type.
182
     *
183
     * @return string
184
     */
185
    public function getIndexColumn()
186
    {
187
        return 'sort_key_int';
188
    }
189
}
190