Completed
Pull Request — master (#93)
by
unknown
23:15
created

EzRelationList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use eZ\Publish\Core\FieldType\RelationList\Value;
6
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
7
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
8
9
class EzRelationList extends AbstractComplexField implements ComplexFieldInterface
10
{
11
    protected $contentMatcher;
12
13
    public function __construct(ContentMatcher $contentMatcher)
14
    {
15 1
        $this->contentMatcher = $contentMatcher;
16
    }
17 1
18
    /**
19 1
     * @param array $fieldValueArray The definition of the field value, structured in the yml file
20 1
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
21
     * @return Value
22 1
     */
23
    public function createValue($fieldValueArray, array $context = array())
24
    {
25 1
        if (array_key_exists('destinationContentIds', $fieldValueArray)) {
26 1
            // fromHash format
27 1
            $ids = $fieldValueArray['destinationContentIds'];
28 1
        } elseif (is_array($fieldValueArray)) {
29 1
            // simplified format
30
            $ids = $fieldValueArray;
31 1
        } else {
32
            $ids = array();
33
        }
34
35
        foreach ($ids as $key => $id) {
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