Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzSelection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 63
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D hashToFieldValue() 0 33 10
A loadFieldSettings() 0 7 1
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 \eZ\Publish\API\Repository\Repository;
8
9
class EzSelection extends AbstractFieldHandler implements FieldValueImporterInterface
10
{
11
    protected $repository;
12
13
    public function __construct(Repository $repository)
14
    {
15
        $this->repository = $repository;
16
    }
17
18
    /**
19
     * Creates a value object to use as the field value when setting a selection field type.
20
     *
21
     * @param $fieldValue int|string|string[]|int[] should all work
22
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
23
     * @return SelectionValue
24
     */
25
    public function hashToFieldValue($fieldValue, array $context = array())
26
    {
27
        if ($fieldValue === null) {
28
            return new SelectionValue();
29
        }
30
31
        // allow user to pass in a single value
32
        if (is_string($fieldValue) || is_int($fieldValue)) {
33
            $fieldValue = array($fieldValue);
34
        }
35
36
        // allow user to pass in selection values by name
37
        $fieldSettings = null;
38
        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...
39
            if (is_string($val)) {
40
                if (ctype_digit($val)) {
41
                    $fieldValue[$key] = (int)$val;
42
                } else {
43
                    if ($fieldSettings === null) {
44
                        $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']);
45
                    }
46
                    foreach($fieldSettings['options'] as $pos => $name) {
47
                        if ($name === $val) {
48
                            $fieldValue[$key] = $pos;
49
                            break;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
56
        return new SelectionValue($fieldValue);
57
    }
58
59
    /**
60
     * @param string $contentTypeIdentifier
61
     * @param string $fieldIdentifier
62
     * @return mixed
63
     */
64
    protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier)
65
    {
66
        $contentTypeService = $this->repository->getContentTypeService();
67
        $contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
68
        $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...
69
        return $fieldDefinition->fieldSettings;
70
    }
71
}
72