Completed
Pull Request — master (#44)
by
unknown
08:30
created

AttributeOptionUpdater::validateDataType()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 32
rs 8.0555
cc 9
nc 9
nop 2
1
<?php
2
3
namespace Flagbit\Bundle\TableAttributeBundle\Updater;
4
5
use Akeneo\Pim\Structure\Component\Model\AttributeOptionInterface;
6
use Akeneo\Pim\Structure\Component\Updater\AttributeOptionUpdater as BaseUpdater;
7
use Akeneo\Tool\Component\StorageUtils\Exception\InvalidPropertyException;
8
use Akeneo\Tool\Component\StorageUtils\Exception\InvalidPropertyTypeException;
9
use Flagbit\Bundle\TableAttributeBundle\Entity\AttributeOption;
10
11
class AttributeOptionUpdater extends BaseUpdater
12
{
13
    protected function validateDataType($field, $data)
14
    {
15
        if (!in_array($field, ['type', 'type_config', 'constraints'])) {
16
            return parent::validateDataType($field, $data);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::validateDataType($field, $data) targeting Akeneo\Pim\Structure\Com...ter::validateDataType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
17
        }
18
19
        switch ($field) {
20
            case 'type':
21
                $availableTypes = ['select', 'select_from_url', 'text', 'number'];
22
                if (!in_array($data, $availableTypes)) {
23
                    InvalidPropertyException::dataExpected('type', implode(', ', $availableTypes), static::class);
24
                }
25
                break;
26
27
            case 'type_config':
28
                if (!is_array($data)) {
29
                    throw InvalidPropertyTypeException::arrayExpected($field, static::class, $data);
30
                }
31
32
                foreach ($data as $key => $value) {
33
                    $this->validateTypeConfig($key, $value);
34
                }
35
                break;
36
37
            case 'constraints':
38
                if (!is_array($data)) {
39
                    throw InvalidPropertyTypeException::arrayExpected($field, static::class, $data);
40
                }
41
                break;
42
43
            default:
44
                break;
45
        }
46
    }
47
48
    protected function validateTypeConfig($key, $value)
49
    {
50
        switch ($key) {
51
            case 'is_decimal':
52
                if (!is_bool($value)) {
53
                    InvalidPropertyTypeException::booleanExpected('type_config-is_decimal', static::class, $value);
54
                }
55
                break;
56
57
            case 'options':
58
                if (!is_array($value)) {
59
                    throw InvalidPropertyTypeException::arrayExpected('type_config-options', static::class, $value);
60
                }
61
                break;
62
63
            case 'options_url':
64
                if (!is_string($value)) {
65
                    InvalidPropertyTypeException::stringExpected('type_config-options_url', static::class, $value);
66
                }
67
                break;
68
69
            default:
70
                break;
71
        }
72
    }
73
74
    protected function setData(AttributeOptionInterface $attributeOption, $field, $data)
75
    {
76
        parent::setData($attributeOption, $field, $data);
77
78
        if ($attributeOption instanceof AttributeOption && $attributeOption->isTableAttribute()) {
79
            if ('type' === $field) {
80
                $attributeOption->setType($data);
81
            }
82
83
            if ('type_config' === $field) {
84
                $attributeOption->setTypeConfig($data);
85
            }
86
87
            if ('constraints' === $field) {
88
                $attributeOption->setConstraints($data);
89
            }
90
        }
91
    }
92
}
93