Passed
Pull Request — master (#225)
by
unknown
09:07
created

EzCountry::loadFieldSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\API\Repository\ContentTypeService;
6
use eZ\Publish\Core\FieldType\Country\Value as CountryValue;
7
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
8
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
9
10
class EzCountry extends AbstractFieldHandler implements FieldValueImporterInterface
11
{
12
    private $contentTypeService;
13
14
    private $countries;
15
16
    public function __construct(ContentTypeService $contentTypeService, array $countries)
17
    {
18
        $this->contentTypeService = $contentTypeService;
19
        $this->countries = $countries;
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. Contains 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
        $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']);
36
37
        $countries = [];
38
        if (!$fieldSettings['isMultiple'] && is_string($fieldValue)) {
39
            $key = $this->findCountryByColumnValue($countries, $fieldValue);
0 ignored issues
show
Bug introduced by
The method findCountryByColumnValue() does not exist on Kaliop\eZMigrationBundle...\FieldHandler\EzCountry. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            /** @scrutinizer ignore-call */ 
40
            $key = $this->findCountryByColumnValue($countries, $fieldValue);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            $countries = [$fieldValue => $this->countries[$key]];
41
            return new CountryValue($countries);
42
        }
43
44
        if (is_string($fieldValue)) {
45
            $fieldValue = array($fieldValue);
46
        }
47
48
        foreach ($fieldValue as $countryCode) {
49
            $key = $this->findCountryByColumnValue($countries, $fieldValue);
50
            $countries[$key] = $this->countries[$key];
51
        }
52
53
        return new CountryValue($countries);
54
    }
55
56
    private function findCountryKeyByValue($countries, $value)
0 ignored issues
show
Unused Code introduced by
The method findCountryKeyByValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
57
    {
58
        if (ctype_digit($value)) {
59
            return array_search($value, array_column('IDC', $countries));
0 ignored issues
show
Bug introduced by
'IDC' of type string is incompatible with the type array expected by parameter $input of array_column(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            return array_search($value, array_column(/** @scrutinizer ignore-type */ 'IDC', $countries));
Loading history...
60
        }
61
62
        if (strlen($value) === 2) {
63
            return array_search($value, array_column('Alpha2', $countries));
64
        }
65
66
        if (strlen($value) === 3) {
67
            return array_search($value, array_column('Alpha3', $countries));
68
        }
69
70
        return array_search($value, array_column('Name', $countries));
71
    }
72
73
    /**
74
     * @param string $contentTypeIdentifier
75
     * @param string $fieldIdentifier
76
     * @return mixed
77
     */
78
    protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier)
79
    {
80
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
81
        $fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
82
        return $fieldDefinition->fieldSettings;
83
    }
84
}
85