Completed
Branch feature/MGFXR-with-attributes-... (b143c2)
by Carlo
02:53
created

DataIterator::_invokeProviderMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
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
        $this->_setProvider($index, $value);
43
        $value = $this->_invokeProviderMethod($value);
44
    }
45
46
    /**
47
     * @return bool
48
     *
49
     */
50
    protected function _canApply()
51
    {
52
        return is_callable(call_user_func(array(get_class($this), 'traverse'), $this));
53
    }
54
55
    /**
56
     * @param Provider $provider
57
     * @param          $method
58
     *
59
     * @throws ProviderMethodNotFound
60
     *
61
     */
62
    protected function _throwMethodNotFoundExceptionForProvider(Provider $provider, $method)
63
    {
64
        throw new ProviderMethodNotFound(
65
            "Given provider does not have the method " . $method . ' -> ' . get_class($provider)
66
        );
67
    }
68
69
    /**
70
     * @param $index
71
     * @param $value
72
     */
73
    protected function _setProvider($index, $value)
74
    {
75
        if ($index == 'data_provider') {
76
            $dataProvider = new \ReflectionClass($value);
77
            $this->_provider = $dataProvider->newInstance();
78
        }
79
    }
80
81
    /**
82
     * @param $value
83
     * @return mixed
84
     * @throws ProviderMethodNotFound
85
     */
86
    protected function _invokeProviderMethod($value)
87
    {
88
        $return = $value;
89
90
        if ($this->_isReplaceableValue($value)) {
91
            $return = $this->_replaceValue($value);
92
        }
93
94
        return $return;
95
    }
96
97
    /**
98
     * @param $method
99
     *
100
     * @return bool
101
     *
102
     */
103
    protected function _isInvokableForProviderInScope($method)
104
    {
105
        return method_exists($this->_provider, $method);
106
    }
107
108
    /**
109
     * @param $value
110
     * @return int
111
     */
112
    protected function _isReplaceableValue($value)
113
    {
114
        return preg_match('/^\{\{.*?\}\}$/i', $value);
115
    }
116
117
    /**
118
     * @param $value
119
     * @return mixed
120
     * @throws ProviderMethodNotFound
121
     */
122
    protected function _replaceValue($value)
123
    {
124
        $method = trim($value, '{}');
125
126
        if (!$this->_isInvokableForProviderInScope($method)) {
127
            $this->_throwMethodNotFoundExceptionForProvider($this->_provider, $method);
128
        }
129
130
        return $this->_provider->$method();
131
    }
132
}
133