BundleSelectionsIterator::traverse()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.2
cc 4
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace Magefix\Fixture\Builder\Helper;
4
5
/**
6
 * Class BundleSelectionsIterator
7
 * @package Magefix\Fixture\Builder\Helper
8
 * @author  Carlo Tasca <[email protected]>
9
 */
10
class BundleSelectionsIterator extends \ArrayIterator
11
{
12
    private $_bundleProducts = [];
13
14
    /**
15
     * @param array $bundleProducts
16
     */
17
    public function setBundleProducts($bundleProducts)
18
    {
19
        $this->_bundleProducts = $bundleProducts;
20
    }
21
22
23
24
    public function apply()
25
    {
26
        $data = $this->getArrayCopy();
27
        array_walk_recursive($data, [$this, 'traverse']);
28
29
        return $data;
30
    }
31
32
    /**
33
     * array_walk_recursive callback
34
     *
35
     * @param $value
36
     */
37
    public function traverse(&$value, $index)
38
    {
39
        if ($index == 'product_id') {
40
            $productId = trim($value, '#');
41
            if (is_numeric($productId) && isset($this->_bundleProducts[$productId])) {
42
                $value = $this->_bundleProducts[$productId]->getId();
43
            }
44
        }
45
    }
46
}
47