Passed
Push — master ( 955189...d6ce84 )
by Gaetano
08:05 queued 17s
created

EzCountry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 34
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hashToFieldValue() 0 15 3
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 eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
9
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
10
11
class EzCountry extends AbstractFieldHandler implements FieldValueImporterInterface
12
{
13
    private $contentTypeService;
14
15
    private $fieldTypeService;
16
17
    public function __construct(ContentTypeService $contentTypeService, FieldTypeService $fieldTypeService)
18
    {
19
        $this->contentTypeService = $contentTypeService;
20
        $this->fieldTypeService = $fieldTypeService;
21
    }
22
23
    /**
24
     * Creates a value object to use as the field value when setting a country field type.
25
     *
26
     * @param $fieldValue string|string[] should work
27
     * @param array $context The context for execution of the current migrations. $hashValueContains f.e. the path to the migration
28
     * @return CountryValue
29
     */
30
    public function hashToFieldValue($fieldValue, array $context = array())
31
    {
32
        if ($fieldValue === null) {
33
            return new CountryValue();
34
        }
35
36
        if (is_string($fieldValue)) {
37
            $fieldValue = array($fieldValue);
38
        }
39
40
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($context['contentTypeIdentifier']);
41
        $field = $contentType->getFieldDefinition($context['fieldIdentifier']);
42
        $fieldType = $this->fieldTypeService->getFieldType($field->fieldTypeIdentifier);
43
44
        return $fieldType->fromHash($fieldValue);
45
    }
46
}
47