DataIterator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 13
c 4
b 2
f 1
lcom 1
cbo 1
dl 0
loc 112
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _isInvokableForProviderInScope() 0 4 1
A _isReplaceableValue() 0 4 2
A apply() 0 8 1
A traverse() 0 7 2
A _throwMethodNotFoundExceptionForProvider() 0 6 1
A _setProvider() 0 7 2
A _invokeProviderMethod() 0 10 2
A _replaceValue() 0 10 2
1
<?php
2
3
namespace Magefix\Fixture;
4
5
use Magefix\Exceptions\ProviderMethodNotFound;
6
use Magefix\Fixtures\Data\Provider;
7
8
/**
9
 * Class DataIterator
10
 *
11
 * @package Magefix\Fixture\Attributes
12
 * @author  Carlo Tasca <[email protected]>
13
 */
14
class DataIterator extends \ArrayIterator
15
{
16
    /**
17
     * @var Provider
18
     */
19
    private $_provider;
20
21
    /**
22
     * @param Provider $_provider
23
     * @return array
24
     */
25
    public function apply(Provider $_provider)
26
    {
27
        $this->_provider = $_provider;
28
        $data = $this->getArrayCopy();
29
        array_walk_recursive($data, [$this, 'traverse']);
30
31
        return $data;
32
    }
33
34
    /**
35
     * array_walk_recursive callback
36
     *
37
     * @param $value
38
     * @param $index
39
     */
40
    public function traverse(&$value, $index)
41
    {
42
        if (!is_numeric($index)) {
43
            $this->_setProvider($index, $value);
44
            $value = $this->_invokeProviderMethod($value);
45
        }
46
    }
47
48
    /**
49
     * @param Provider $provider
50
     * @param          $method
51
     *
52
     * @throws ProviderMethodNotFound
53
     *
54
     */
55
    protected function _throwMethodNotFoundExceptionForProvider(Provider $provider, $method)
56
    {
57
        throw new ProviderMethodNotFound(
58
            "Given provider does not have the method " . $method . ' -> ' . get_class($provider)
59
        );
60
    }
61
62
    /**
63
     * @param $index
64
     * @param $value
65
     */
66
    protected function _setProvider($index, $value)
67
    {
68
        if ($index == 'data_provider') {
69
            $dataProvider = new \ReflectionClass($value);
70
            $this->_provider = $dataProvider->newInstance();
71
        }
72
    }
73
74
    /**
75
     * @param $value
76
     * @return mixed
77
     * @throws ProviderMethodNotFound
78
     */
79
    protected function _invokeProviderMethod($value)
80
    {
81
        $return = $value;
82
83
        if ($this->_isReplaceableValue($value) !== false) {
84
            $return = $this->_replaceValue($value);
85
        }
86
87
        return $return;
88
    }
89
90
    /**
91
     * @param $method
92
     *
93
     * @return bool
94
     *
95
     */
96
    protected function _isInvokableForProviderInScope($method)
97
    {
98
        return method_exists($this->_provider, $method);
99
    }
100
101
    /**
102
     * @param $value
103
     * @return int|bool
104
     */
105
    protected function _isReplaceableValue($value)
106
    {
107
        return is_string($value) && preg_match('/^\{\{.*?\}\}$/i', $value);
108
    }
109
110
    /**
111
     * @param $value
112
     * @return mixed
113
     * @throws ProviderMethodNotFound
114
     */
115
    protected function _replaceValue($value)
116
    {
117
        $method = trim($value, '{}');
118
119
        if (!$this->_isInvokableForProviderInScope($method)) {
120
            $this->_throwMethodNotFoundExceptionForProvider($this->_provider, $method);
121
        }
122
123
        return $this->_provider->$method();
124
    }
125
}
126