FromArrayAssembler::mapInstanceProperties()   B
last analyzed

Complexity

Conditions 9
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.0555
c 0
b 0
f 0
cc 9
nc 3
nop 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-05-26 
5
 */
6
7
namespace Net\Bazzline\Component\Locator\Configuration\Assembler;
8
9
use Net\Bazzline\Component\Locator\Configuration\Configuration;
10
11
/**
12
 * Class FromArrayAssembler
13
 * @package Net\Bazzline\Component\Locator\Configuration\Assembler
14
 */
15
class FromArrayAssembler extends AbstractAssembler
16
{
17
    /**
18
     * @param mixed $data
19
     * @param Configuration $configuration
20
     * @return Configuration
21
     * @throws RuntimeException
22
     */
23
    protected function map($data, Configuration $configuration)
24
    {
25
        $configuration = $this->mapBooleanProperties(
26
            $data,
27
            $configuration
28
        );
29
        $configuration = $this->mapStringProperties(
30
            $data,
31
            $configuration
32
        );
33
        $configuration = $this->mapInstanceProperties(
34
            $data,
35
            $configuration
36
        );
37
        $configuration = $this->mapArrayProperties(
38
            $data,
39
            $configuration
40
        );
41
42
        return $configuration;
43
    }
44
45
    /**
46
     * @param mixed $data
47
     * @throws InvalidArgumentException
48
     */
49 View Code Duplication
    protected function validateData($data)
50
    {
51
        if (!is_array($data)) {
52
            throw new InvalidArgumentException(
53
                'data must be an array'
54
            );
55
        }
56
57
        if (empty($data)) {
58
            throw new InvalidArgumentException(
59
                'data array must contain content'
60
            );
61
        }
62
63
        $mandatoryKeysToExpectedValueTyp = array(
64
            'class_name'        => 'string',
65
            'file_path'         => 'string'
66
        );
67
68
        $this->validateDataWithMandatoryKeysAndExpectedValueType(
69
            $data,
70
            $mandatoryKeysToExpectedValueTyp
71
        );
72
73
        $optionalKeysToExpectedValueTyp = array(
74
            'extends'           => 'string',
75
            'instances'         => 'array',
76
            'implements'        => 'array',
77
            'namespace'         => 'string',
78
            'uses'              => 'array'
79
        );
80
81
        $this->validateDataWithOptionalKeysAndExpectedValueTypeOrSetExpectedValueAsDefault(
82
            $data,
83
            $optionalKeysToExpectedValueTyp
84
        );
85
    }
86
87
    /**
88
     * @param array $data
89
     * @param Configuration $configuration
90
     * @return Configuration
91
     */
92 View Code Duplication
    private function mapArrayProperties(array $data, Configuration $configuration)
93
    {
94
        if (isset($data['implements'])) {
95
            foreach ($data['implements'] as $interfaceName) {
96
                $configuration->addImplements($interfaceName);
97
            }
98
        }
99
100
        if (isset($data['uses'])) {
101
            foreach ($data['uses'] as $key => $uses) {
102
                if (!isset($uses['class_name'])) {
103
                    throw new RuntimeException(
104
                        'use entry with key "' . $key . '" needs to have a key "class_name"'
105
                    );
106
                }
107
108
                $alias  = (isset($uses['alias'])) ? $uses['alias'] : '';
109
                $class  = $uses['class_name'];
110
                $configuration->addUses($class, $alias);
111
            }
112
        }
113
114
        return $configuration;
115
    }
116
117
    /**
118
     * @param array $data
119
     * @param Configuration $configuration
120
     * @return Configuration
121
     */
122
    private function mapInstanceProperties(array $data, Configuration $configuration)
123
    {
124
        if (isset($data['instances'])) {
125
            foreach ($data['instances'] as $key => $instance) {
126
                if (!isset($instance['class_name'])) {
127
                    throw new RuntimeException('instance entry with key "' . $key . '" needs to have a key "class_name"');
128
                }
129
130
                $alias              = (isset($instance['alias'])) ? $instance['alias'] : null;
131
                $class              = $instance['class_name'];
132
                $isFactory          = (isset($instance['is_factory'])) ? $instance['is_factory'] : false;
133
                $isShared           = (isset($instance['is_shared'])) ? $instance['is_shared'] : true;
134
                $methodBodyBuilder  = (isset($instance['method_body_builder'])) ? $instance['method_body_builder'] : null;
135
                $returnValue        = (isset($instance['return_value'])) ? $instance['return_value'] : $class;
136
137
                $configuration->addInstance($class, $isFactory, $isShared, $returnValue, $alias, $methodBodyBuilder);
138
            }
139
        }
140
141
        return $configuration;
142
    }
143
}