Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzRelation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 52
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hashToFieldValue() 0 21 4
A fieldSettingsToHash() 0 8 4
A hashToFieldSettings() 0 4 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\Relation\Value;
6
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
7
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
8
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
9
10
class EzRelation extends AbstractFieldHandler implements FieldValueImporterInterface, FieldDefinitionConverterInterface
11
{
12
    protected $contentMatcher;
13
14
    public function __construct(ContentMatcher $contentMatcher)
15
    {
16
        $this->contentMatcher = $contentMatcher;
17
    }
18
19
    /**
20
     * Creates a value object to use as the field value when setting an ez relation field type.
21
     *
22
     * @param array|string|int $fieldValue The definition of the field value, structured in the yml file
23
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
24
     * @return Value
25
     */
26
    public function hashToFieldValue($fieldValue, array $context = array())
27
    {
28
        if (is_array($fieldValue) && array_key_exists('destinationContentId', $fieldValue)) {
29
            // fromHash format
30
            $id = $fieldValue['destinationContentId'];
31
        } else {
32
            // simplified format
33
            $id = $fieldValue;
34
        }
35
36
        if ($id === null) {
37
            return new Value();
38
        }
39
40
        // 1. resolve relations
41
        $id = $this->referenceResolver->resolveReference($id);
42
        // 2. resolve remote ids
43
        $id = $this->contentMatcher->matchOneByKey($id)->id;
44
45
        return new Value($id);
46
    }
47
48
    public function fieldSettingsToHash($settingsValue, array $context = array())
49
    {
50
        // work around https://jira.ez.no/browse/EZP-26916
51
        if (is_array($settingsValue) && isset($settingsValue['selectionRoot']) && $settingsValue['selectionRoot'] === '') {
52
            $settingsValue['selectionRoot'] = null;
53
        }
54
        return $settingsValue;
55
    }
56
57
    public function hashToFieldSettings($settingsHash, array $context = array())
58
    {
59
        return $settingsHash;
60
    }
61
}
62