Test Failed
Pull Request — master (#4)
by
unknown
11:21
created

RelationConfig::setTargetObjectTypes()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 6
nop 1
1
<?php
2
3
namespace Charcoal\Relation;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-config'
8
use Charcoal\Config\AbstractConfig;
9
10
/**
11
 * Relation Configset
12
 */
13
class RelationConfig extends AbstractConfig
14
{
15
    /**
16
     * Available target object types.
17
     *
18
     * @var array
19
     */
20
    private $targetObjectTypes = [];
21
22
    /**
23
     * Set relation settings in a specific order.
24
     *
25
     * @param  array $data New config values.
26
     * @return RelationConfig Chainable
27
     */
28
    public function setData(array $data)
29
    {
30
        if (isset($data['targetObjectTypes'])) {
31
            $this->setTargetObjectTypes($data['targetObjectTypes']);
32
        }
33
34
        unset($data['targetObjectTypes'], $data['groups'], $data['widgets']);
35
36
        return parent::setData($data);
37
    }
38
39
    /**
40
     * Retrieve the available target object types.
41
     *
42
     * @return array
43
     */
44
    public function targetObjectTypes()
45
    {
46
        return $this->targetObjectTypes;
47
    }
48
49
    /**
50
     * Set target object types.
51
     *
52
     * @param  array $targetObjectTypes One or more object types.
53
     * @throws InvalidArgumentException If the object type or structure is invalid.
54
     * @return RelationConfig Chainable
55
     */
56
    public function setTargetObjectTypes(array $targetObjectTypes)
57
    {
58
        foreach ($targetObjectTypes as $type => $struct) {
59
            if (!is_array($struct)) {
60
                throw new InvalidArgumentException(sprintf(
61
                    'The object structure for "%s" must be an array',
62
                    $type
63
                ));
64
            }
65
66
            if (isset($struct['object_type'])) {
67
                $type = $struct['object_type'];
68
            } else {
69
                $struct['object_type'] = $type;
70
            }
71
72
            if (!is_string($type)) {
73
                throw new InvalidArgumentException(
74
                    'The object type must be a string'
75
                );
76
            }
77
78
            $this->targetObjectTypes[$type] = $struct;
79
        }
80
81
        return $this;
82
    }
83
}
84