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

RelationProcessor   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 167
Duplicated Lines 19.16 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 31
lcom 1
cbo 10
dl 32
loc 167
rs 9.8
c 2
b 2
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
F processFieldRelations() 32 89 20
A __construct() 0 4 1
D appendFieldRelations() 0 37 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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