GroupedProduct::_buildGroupedSimpleProducts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Magefix\Fixture\Builder;
4
5
use Mage;
6
use Mage_Catalog_Model_Product_Type;
7
8
/**
9
 * Class GroupedProduct
10
 * @package Magefix\Fixture\Builder
11
 * @author  Carlo Tasca <[email protected]>
12
 */
13
class GroupedProduct extends Product
14
{
15
    private $_groupedProducts = [];
16
    private $_groupedProductsIds = [];
17
18
    public function build()
19
    {
20
        $mergedData = $this->_beforeBuild();
21
        $this->_buildGroupedSimpleProducts();
22
        $this->_getMageModel()->setData($mergedData);
23
        $fixtureId = $this->_saveFixture();
24
        $this->_linkSimpleProducts($fixtureId);
25
26
        return $fixtureId;
27
    }
28
29
    /**
30
     * @param int $groupedProductId
31
     */
32
    protected function _linkSimpleProducts($groupedProductId)
33
    {
34
        $this->_prepareProductIds();
35
        $productLinkApi = Mage::getModel('catalog/product_link_api');
36
        $iterator = new \ArrayIterator($this->_groupedProductsIds);
37
        while ($iterator->valid()) {
38
            $productLinkApi->assign(
39
                Mage_Catalog_Model_Product_Type::TYPE_GROUPED, $groupedProductId, $iterator->current()
40
            );
41
            $iterator->next();
42
        }
43
    }
44
45
    protected function _buildGroupedSimpleProducts()
46
    {
47
        $this->_groupedProducts = $this->_buildManySimple('grouped_products');
48
    }
49
50
    protected function _prepareProductIds()
51
    {
52
        $iterator = new \ArrayIterator($this->_groupedProducts);
53
        while ($iterator->valid()) {
54
            $current = $iterator->current();
55
            $this->_groupedProductsIds[] = $current->getId();
56
            $iterator->next();
57
        }
58
    }
59
60
}
61