Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

EzSelection::hashToFieldValue()   B

Complexity

Conditions 10
Paths 19

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 28
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 19
nop 2
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
7
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
8
use \eZ\Publish\API\Repository\Repository;
9
10
class EzSelection extends AbstractFieldHandler implements FieldValueImporterInterface, FieldDefinitionConverterInterface
11
{
12
    protected $repository;
13
14
    public function __construct(Repository $repository)
15
    {
16
        $this->repository = $repository;
17
    }
18
19
    /**
20
     * Creates a value object to use as the field value when setting a selection field type.
21
     *
22
     * @param $fieldValue int|string|string[]|int[] should all work
23
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
24
     * @return SelectionValue
25
     */
26
    public function hashToFieldValue($fieldValue, array $context = array())
27
    {
28
        if ($fieldValue === null) {
29
            return new SelectionValue();
30
        }
31
32
        // allow user to pass in a single value
33
        if (is_string($fieldValue) || is_int($fieldValue)) {
34
            $fieldValue = array($fieldValue);
35
        }
36
37
        // allow user to pass in selection values by name
38
        $fieldSettings = null;
39
        foreach($fieldValue as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $fieldValue of type object|double|null|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
40
            if (is_string($val)) {
41
                if (ctype_digit($val)) {
42
                    $fieldValue[$key] = (int)$val;
43
                } else {
44
                    if ($fieldSettings === null) {
45
                        $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']);
46
                    }
47
                    foreach($fieldSettings['options'] as $pos => $name) {
48
                        if ($name === $val) {
49
                            $fieldValue[$key] = $pos;
50
                            break;
51
                        }
52
                    }
53
                }
54
            }
55
        }
56
57
        return new SelectionValue($fieldValue);
58
    }
59
60
    /**
61
     * @param string $contentTypeIdentifier
62
     * @param string $fieldIdentifier
63
     * @return mixed
64
     */
65
    protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier)
66
    {
67
        $contentTypeService = $this->repository->getContentTypeService();
68
        $contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
69
        $fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
0 ignored issues
show
Bug introduced by
The property fieldDefinitionsByIdentifier does not seem to exist. Did you mean fieldDefinitions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
        return $fieldDefinition->fieldSettings;
71
    }
72
73
    public function fieldSettingsToHash($settingsValue, array $context = array())
74
    {
75
        return $settingsValue;
76
    }
77
78
    public function hashToFieldSettings($settingsHash, array $context = array())
79
    {
80
        foreach ($settingsHash['options'] as $key => $value) {
81
            if (!is_int($key) && !ctype_digit($key)) {
82
                throw new \Exception("The list of values allowed for an eZSelection field can only use integer keys, found: '$key'");
83
            }
84
        }
85
        return $settingsHash;
86
    }
87
}
88