Completed
Push — 19.x ( c94702...e76808 )
by Tim
09:06
created

ProductValueLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * TechDivision\Import\Loaders\ProductValueLoader
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Loaders;
22
23
use TechDivision\Import\Loaders\LoaderInterface;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface;
26
27
/**
28
 * Generic loader for product values.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductValueLoader implements LoaderInterface
37
{
38
39
    /**
40
     * The column name to load the values for.
41
     *
42
     * @var string
43
     */
44
    protected $columnName;
45
46
    /**
47
     * The available values.
48
     *
49
     * @var array
50
     */
51
    protected $values = array();
52
53
    /**
54
     * The registry loader instance.
55
     *
56
     * @var \TechDivision\Import\Loaders\LoaderInterface
57
     */
58
    protected $registryLoader;
59
60
    /**
61
     * Construct that initializes the iterator with the product processor instance.
62
     *
63
     * @param \TechDivision\Import\Loaders\LoaderInterface                         $registryLoader   The registry loader instance
64
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productProcessor The product processor instance
65
     * @param string                                                               $columName        The column name to load the values for
66
     */
67
    public function __construct(LoaderInterface $registryLoader, ProductBunchProcessorInterface $productProcessor, $columName = MemberNames::SKU)
68
    {
69
70
        // initialize the column name and the registry loader
71
        $this->columnName = $columName;
72
        $this->registryLoader = $registryLoader;
73
74
        // initialize the array with the SKUs
75
        foreach ($productProcessor->loadProducts() as $product) {
76
            $this->skus[] = $product[$this->columnName];
77
        }
78
    }
79
80
    /**
81
     * Loads and returns data.
82
     *
83
     * @return \ArrayAccess The array with the data
84
     */
85
    public function load()
86
    {
87
        // load the already processed SKUs from the registry, merge
88
        // them with the ones from the DB and return them
89
        $collectedColumns = $this->getRegistryLoader()->load();
90
91
        // query whether or not values for the configured column name are available
92
        if (is_array($collectedColumns[$this->columnName])) {
93
            // if yes merge the values into the array with the values from the DB
94
            foreach ($collectedColumns[$this->columnName] as $value) {
95
                // query whether or not the value already exits
96
                if (in_array($value, $this->values)) {
97
                    continue;
98
                }
99
100
                // append the value in the array
101
                $this->values[] = $value;
102
            }
103
        }
104
105
        // return the values
106
        return $this->values;
107
    }
108
109
    /**
110
     * The registry loader instance.
111
     *
112
     * @return \TechDivision\Import\Loaders\LoaderInterface The loader instance
113
     */
114
    protected function getRegistryLoader()
115
    {
116
        return $this->registryLoader;
117
    }
118
}
119