ConverterSubject::getArtefacts()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 13
rs 10
ccs 0
cts 5
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Converter\Product\Attribute\Subjects\ProductCategoryConverterSubject
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-converter-product-attribute
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Converter\Product\Attribute\Subjects;
16
17
use TechDivision\Import\Utils\CacheKeys;
18
use TechDivision\Import\Product\Utils\ColumnKeys;
19
20
/**
21
 * The subject implementation that handles the business logic to persist products.
22
 *
23
 * @author    Tim Wagner <[email protected]>
24
 * @copyright 2019 TechDivision GmbH <[email protected]>
25
 * @license   https://opensource.org/licenses/MIT
26
 * @link      https://github.com/techdivision/import-converter-product-attribute
27
 * @link      http://www.techdivision.com
28
 */
29
class ConverterSubject extends \TechDivision\Import\Converter\Subjects\ConverterSubject
30
{
31
32
    /**
33
     * Return's the artefacts for post-processing.
34
     *
35
     * @return array The artefacts
36
     */
37
    public function getArtefacts()
38
    {
39
40
        // initialize the array for the artefacts
41
        $artefacts = array();
42
43
        // query whether or not artefacts are available
44
        if (is_array($arts = $this->getRegistryProcessor()->getAttribute(CacheKeys::ARTEFACTS))) {
45
            $artefacts = $arts;
46
        }
47
48
        // return the artefacts
49
        return $artefacts;
50
    }
51
52
    /**
53
     * Add the passed product type artefacts to the product with the
54
     * last entity ID and overrides existing ones with the same key.
55
     *
56
     * @param string $type      The artefact type, e. g. configurable
57
     * @param array  $artefacts The product type artefacts
58
     *
59
     * @return void
60
     */
61
    protected function overrideArtefacts($type, array $artefacts)
62
    {
63
64
        // initialize the array for the artefacts that has to be merged
65
        $toBeMerged = array();
66
67
        // add the artefacts by using their array key to override them
68
        foreach ($artefacts as $key => $artefact) {
69
            $toBeMerged[$type][$this->getLastEntityId()][$key] = $artefact;
70
        }
71
72
        // replace the artefacts in the registry
73
        $this->getRegistryProcessor()->mergeAttributesRecursive(CacheKeys::ARTEFACTS, $toBeMerged);
74
    }
75
76
    /**
77
     * Append's the passed product type artefacts to the product with the
78
     * last entity ID.
79
     *
80
     * @param string $type      The artefact type, e. g. configurable
81
     * @param array  $artefacts The product type artefacts
82
     *
83
     * @return void
84
     */
85
    protected function appendArtefacts($type, array $artefacts)
86
    {
87
88
        // initialize the array for the artefacts that has to be merged
89
        $toBeMerged = array();
90
91
        // append the artefacts
92
        foreach ($artefacts as $artefact) {
93
            $toBeMerged[$type][$this->getLastEntityId()][] = $artefact;
94
        }
95
96
        // replace the artefacts in the registry
97
        $this->getRegistryProcessor()->mergeAttributesRecursive(CacheKeys::ARTEFACTS, $toBeMerged);
98
    }
99
100
    /**
101
     * Return the artefacts for the passed type and entity ID.
102
     *
103
     * @param string $type     The artefact type, e. g. configurable
104
     * @param string $entityId The entity ID to return the artefacts for
105
     *
106
     * @return array The array with the artefacts
107
     * @throws \Exception Is thrown, if no artefacts are available
108
     */
109
    public function getArtefactsByTypeAndEntityId($type, $entityId)
110
    {
111
112
        // load the available artefacts from the registry
113
        $arts = $this->getRegistryProcessor()->getAttribute(CacheKeys::ARTEFACTS);
114
115
        // query whether or not, artefacts for the passed params are available
116
        if (isset($arts[$type][$entityId])) {
117
            // load the artefacts
118
            $artefacts = $arts[$entityId];
119
120
            // unserialize the original data, if we're in debug mode
121
            $keys = array_keys($artefacts);
122
            foreach ($keys as $key) {
123
                if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) {
124
                    $artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? unserialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null;
125
                }
126
            }
127
128
            // return the artefacts
129
            return $artefacts;
130
        }
131
132
        // throw an exception if not
133
        throw new \Exception(
134
            sprintf(
135
                'Cant\'t load artefacts for type %s and entity ID %d',
136
                $type,
137
                $entityId
138
            )
139
        );
140
    }
141
142
    /**
143
     * Queries whether or not artefacts for the passed type and entity ID are available.
144
     *
145
     * @param string $type     The artefact type, e. g. configurable
146
     * @param string $entityId The entity ID to return the artefacts for
147
     *
148
     * @return boolean TRUE if artefacts are available, else FALSE
149
     */
150
    public function hasArtefactsByTypeAndEntityId($type, $entityId)
151
    {
152
153
        // load the available artefacts from the registry
154
        $arts = $this->getRegistryProcessor()->getAttribute(CacheKeys::ARTEFACTS);
155
156
        // query whether or not artefacts for the passed type and entity ID are available
157
        return isset($arts[$type][$entityId]);
158
    }
159
160
    /**
161
     * Export's the artefacts to CSV files and resets the array with the artefacts to free the memory.
162
     *
163
     * @param integer $timestamp The timestamp part of the original import file
164
     * @param string  $counter   The counter part of the origin import file
165
     *
166
     * @return void
167
     */
168
    public function export($timestamp, $counter)
169
    {
170
        // do nothing here, because we want to plug-in to export the categories
171
    }
172
173
    /**
174
     * Return's the ID of the product that has been created recently.
175
     *
176
     * @return string The entity Id
177
     */
178
    public function getLastEntityId()
179
    {
180
        return $this->getValue(ColumnKeys::SKU);
181
    }
182
}
183