Completed
Push — ezp25366-copy_content_relation... ( 7d5327...4b97d3 )
by André
74:42 queued 12:30
created

RelationProcessor::processFieldRelations()   F

Complexity

Conditions 20
Paths 378

Size

Total Lines 89
Code Lines 57

Duplication

Lines 32
Ratio 35.96 %
Metric Value
dl 32
loc 89
rs 3.6338
cc 20
eloc 57
nc 378
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the RelationProcessor 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Repository\Helper;
12
13
use eZ\Publish\SPI\Persistence\Handler;
14
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
15
use eZ\Publish\Core\Repository\Values\Content\Relation;
16
use eZ\Publish\Core\FieldType\Value as BaseValue;
17
use eZ\Publish\SPI\FieldType\FieldType as SPIFieldType;
18
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as SPIRelationCreateStruct;
19
20
/**
21
 * RelationProcessor is an internal service used for handling field relations upon Content creation or update.
22
 *
23
 * @internal
24
 */
25
class RelationProcessor
26
{
27
    /**
28
     * @var \eZ\Publish\SPI\Persistence\Handler
29
     */
30
    protected $persistenceHandler;
31
32
    /**
33
     * Setups service with reference to repository object that created it & corresponding handler.
34
     *
35
     * @param \eZ\Publish\SPI\Persistence\Handler $handler
36
     */
37
    public function __construct(Handler $handler)
38
    {
39
        $this->persistenceHandler = $handler;
40
    }
41
42
    /**
43
     * Appends destination Content ids of given $fieldValue to the $relation array.
44
     *
45
     * If $fieldValue contains Location ids, the will be converted to the Content id that Location encapsulates.
46
     *
47
     * @param array $relations
48
     * @param array $locationIdToContentIdMapping An array with Location Ids as keys and corresponding Content Id as values
49
     * @param \eZ\Publish\SPI\FieldType\FieldType $fieldType
50
     * @param \eZ\Publish\Core\FieldType\Value $fieldValue Accepted field value.
51
     * @param string $fieldDefinitionId
52
     */
53
    public function appendFieldRelations(
54
        array &$relations,
55
        array &$locationIdToContentIdMapping,
56
        SPIFieldType $fieldType,
57
        BaseValue $fieldValue,
58
        $fieldDefinitionId
59
    ) {
60
        foreach ($fieldType->getRelations($fieldValue) as $relationType => $destinationIds) {
61
            if ($relationType === Relation::FIELD) {
62
                if (!isset($relations[$relationType][$fieldDefinitionId])) {
63
                    $relations[$relationType][$fieldDefinitionId] = array();
64
                }
65
                $relations[$relationType][$fieldDefinitionId] += array_flip($destinationIds);
66
            } elseif ($relationType & (Relation::LINK | Relation::EMBED)) {
67
                // Using bitwise operators as Legacy Stack stores COMMON, LINK and EMBED relation types
68
                // in the same entry using bitmask
69
                if (!isset($relations[$relationType])) {
70
                    $relations[$relationType] = array();
71
                }
72
73
                if (isset($destinationIds['locationIds'])) {
74
                    foreach ($destinationIds['locationIds'] as $locationId) {
75
                        if (!isset($locationIdToContentIdMapping[$locationId])) {
76
                            $location = $this->persistenceHandler->locationHandler()->load($locationId);
77
                            $locationIdToContentIdMapping[$locationId] = $location->contentId;
78
                        }
79
80
                        $relations[$relationType][$locationIdToContentIdMapping[$locationId]] = true;
81
                    }
82
                }
83
84
                if (isset($destinationIds['contentIds'])) {
85
                    $relations[$relationType] += array_flip($destinationIds['contentIds']);
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * Persists relation data for a content version.
93
     *
94
     * This method creates new relations and deletes removed relations.
95
     *
96
     * @param array $inputRelations
97
     * @param mixed $sourceContentId
98
     * @param mixed $sourceContentVersionNo
99
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
100
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $existingRelations An array of existing relations for Content version (empty when creating new content)
101
     */
102
    public function processFieldRelations(
103
        array $inputRelations,
104
        $sourceContentId,
105
        $sourceContentVersionNo,
106
        ContentType $contentType,
107
        array $existingRelations = array()
108
    ) {
109
        // Map existing relations for easier handling
110
        $mappedRelations = array();
111
        foreach ($existingRelations as $relation) {
112
            if ($relation->type === Relation::FIELD) {
113
                $fieldDefinitionId = $contentType->getFieldDefinition($relation->sourceFieldDefinitionIdentifier)->id;
114
                $mappedRelations[$relation->type][$fieldDefinitionId][$relation->destinationContentInfo->id] = $relation;
115
            }
116
            // Using bitwise AND as Legacy Stack stores COMMON, LINK and EMBED relation types
117
            // in the same entry using bitmask
118 View Code Duplication
            if ($relation->type & Relation::LINK) {
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...
119
                $mappedRelations[Relation::LINK][$relation->destinationContentInfo->id] = $relation;
120
            }
121 View Code Duplication
            if ($relation->type & Relation::EMBED) {
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...
122
                $mappedRelations[Relation::EMBED][$relation->destinationContentInfo->id] = $relation;
123
            }
124
        }
125
126
        // Add new relations
127
        foreach ($inputRelations as $relationType => $relationData) {
128
            if ($relationType === Relation::FIELD) {
129
                foreach ($relationData as $fieldDefinitionId => $contentIds) {
130
                    foreach (array_keys($contentIds) as $destinationContentId) {
131
                        if (isset($mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId])) {
132
                            unset($mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId]);
133 View Code Duplication
                        } else {
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...
134
                            $this->persistenceHandler->contentHandler()->addRelation(
135
                                new SPIRelationCreateStruct(
136
                                    array(
137
                                        'sourceContentId' => $sourceContentId,
138
                                        'sourceContentVersionNo' => $sourceContentVersionNo,
139
                                        'sourceFieldDefinitionId' => $fieldDefinitionId,
140
                                        'destinationContentId' => $destinationContentId,
141
                                        'type' => $relationType,
142
                                    )
143
                                )
144
                            );
145
                        }
146
                    }
147
                }
148
            } elseif ($relationType === Relation::LINK || $relationType === Relation::EMBED) {
149
                foreach (array_keys($relationData) as $destinationContentId) {
150
                    if (isset($mappedRelations[$relationType][$destinationContentId])) {
151
                        unset($mappedRelations[$relationType][$destinationContentId]);
152 View Code Duplication
                    } else {
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...
153
                        $this->persistenceHandler->contentHandler()->addRelation(
154
                            new SPIRelationCreateStruct(
155
                                array(
156
                                    'sourceContentId' => $sourceContentId,
157
                                    'sourceContentVersionNo' => $sourceContentVersionNo,
158
                                    'sourceFieldDefinitionId' => null,
159
                                    'destinationContentId' => $destinationContentId,
160
                                    'type' => $relationType,
161
                                )
162
                            )
163
                        );
164
                    }
165
                }
166
            }
167
        }
168
169
        // Remove relations not present in input set
170
        foreach ($mappedRelations as $relationType => $relationData) {
171
            foreach ($relationData as $relationEntry) {
172
                switch ($relationType) {
173
                    case Relation::FIELD:
174
                        foreach ($relationEntry as $relation) {
175
                            $this->persistenceHandler->contentHandler()->removeRelation(
176
                                $relation->id,
177
                                $relationType
178
                            );
179
                        }
180
                        break;
181
                    case Relation::LINK:
182
                    case Relation::EMBED:
183
                        $this->persistenceHandler->contentHandler()->removeRelation(
184
                            $relationEntry->id,
185
                            $relationType
186
                        );
187
                }
188
            }
189
        }
190
    }
191
}
192