Passed
Push — master ( d6ce84...9c42e9 )
by Gaetano
10:07
created

EzCountry::doPreResolveStringReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\API\Repository\ContentTypeService;
6
use eZ\Publish\API\Repository\FieldTypeService;
7
use eZ\Publish\Core\FieldType\Country\Value as CountryValue;
8
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
9
10
class EzCountry extends AbstractFieldHandler implements FieldValueImporterInterface
11
{
12
    private $contentTypeService;
13
14
    private $fieldTypeService;
15
16
    public function __construct(ContentTypeService $contentTypeService, FieldTypeService $fieldTypeService)
17
    {
18
        $this->contentTypeService = $contentTypeService;
19
        $this->fieldTypeService = $fieldTypeService;
20
    }
21
22
    /**
23
     * Creates a value object to use as the field value when setting a country field type.
24
     *
25
     * @param $fieldValue string|string[] should work
26
     * @param array $context The context for execution of the current migrations. $hashValueContains f.e. the path to the migration
27
     * @return CountryValue
28
     */
29
    public function hashToFieldValue($fieldValue, array $context = array())
30
    {
31
        if ($fieldValue === null) {
32
            return new CountryValue();
33
        }
34
35
        $refsResolved = false;
36
        if (is_string($fieldValue)) {
37
            $fieldValue = $this->referenceResolver->resolveReference($fieldValue);
38
            $refsResolved = true;
39
        }
40
        if (!is_array($fieldValue)) {
41
            $fieldValue = array($fieldValue);
42
        }
43
        if (!$refsResolved) {
44
            foreach($fieldValue as $key => $countryValue) {
45
                $fieldValue[$key] = $this->referenceResolver->resolveReference($countryValue);
46
            }
47
        }
48
49
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($context['contentTypeIdentifier']);
50
        $field = $contentType->getFieldDefinition($context['fieldIdentifier']);
51
        $fieldType = $this->fieldTypeService->getFieldType($field->fieldTypeIdentifier);
52
53
        return $fieldType->fromHash($fieldValue);
54
    }
55
56
    // To avoid recursive expansion of references, we have to implement resolving logic only within this class
57
    public function doPreResolveStringReferences()
58
    {
59
        return false;
60
    }
61
}
62