Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

EzRelationList::hashToFieldValue()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21

Duplication

Lines 6
Ratio 28.57 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 6
loc 21
ccs 0
cts 15
cp 0
rs 9.2728
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 30
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\RelationList\Value;
6
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
7
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
8
9
class EzRelationList extends AbstractFieldHandler implements FieldValueImporterInterface
10
{
11
    protected $contentMatcher;
12
13
    public function __construct(ContentMatcher $contentMatcher)
14
    {
15
        $this->contentMatcher = $contentMatcher;
16
    }
17
18
    /**
19
     * @param array $fieldValue The definition of the field value, structured in the yml file
20
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
21
     * @return Value
22
     */
23
    public function hashToFieldValue($fieldValue, array $context = array())
24
    {
25
        if (count($fieldValue) == 1 && isset($fieldValue['destinationContentIds'])) {
26
            // fromHash format
27
            $ids = $fieldValue['destinationContentIds'];
28
        } else if ($fieldValue === null) {
29
            $ids = array();
30
        } else {
31
            // simplified format
32
            $ids = $fieldValue;
33
        }
34
35 View Code Duplication
        foreach ($ids as $key => $id) {
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...
36
            // 1. resolve relations
37
            $ids[$key] = $this->referenceResolver->resolveReference($id);
38
            // 2. resolve remote ids
39
            $ids[$key] = $this->contentMatcher->matchOneByKey($ids[$key])->id;
40
        }
41
42
        return new Value($ids);
43
    }
44
}
45