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

EzCountry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
dl 0
loc 50
rs 10
c 1
b 0
f 1
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A doPreResolveStringReferences() 0 3 1
A hashToFieldValue() 0 25 6
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