Completed
Push — 8.x ( 824af6 )
by Tim
09:11
created

ExportableTrait::addArtefacts()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 10
cts 11
cp 0.9091
rs 9.568
cc 4
nc 3
nop 3
crap 4.0119
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\ExportableTrait
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 2016 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\Subjects;
22
23
use TechDivision\Import\Utils\ColumnKeys;
24
use TechDivision\Import\Adapter\ExportAdapterInterface;
25
26
/**
27
 * The trait implementation for the artefact export functionality.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
trait ExportableTrait
36
{
37
38
    /**
39
     * The array containing the data for product type configuration (configurables, bundles, etc).
40
     *
41
     * @var array
42
     */
43
    protected $artefacs = array();
44
45
    /**
46
     * The export adapter instance.
47
     *
48
     * @var \TechDivision\Import\Adapter\ExportAdapterInterface
49
     */
50
    protected $exportAdapter;
51
52
    /**
53
     * The ID of the product that has been created recently.
54
     *
55
     * @var string
56
     */
57
    protected $lastEntityId;
58
59
    /**
60
     * Return's the artefacts for post-processing.
61
     *
62
     * @return array The artefacts
63
     */
64 3
    public function getArtefacts()
65
    {
66 3
        return $this->artefacs;
67
    }
68
69
    /**
70
     * Add the passed product type artefacts to the product with the
71
     * last entity ID.
72
     *
73
     * @param string  $type      The artefact type, e. g. configurable
74
     * @param array   $artefacts The product type artefacts
75
     * @param boolean $override  Whether or not the artefacts for the actual entity ID has to be overwritten
76
     *
77
     * @return void
78
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
79
     */
80 4
    public function addArtefacts($type, array $artefacts, $override = true)
81
    {
82
83
        // query whether or not, any artefacts are available
84 4
        if (sizeof($artefacts) === 0) {
85 1
            return;
86
        }
87
88
        // serialize the original data
89 3
        array_walk($artefacts, function (&$artefact) {
90 3
            if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
91 1
                $artefact[ColumnKeys::ORIGINAL_DATA] = serialize($artefact[ColumnKeys::ORIGINAL_DATA]);
92
            }
93 3
        });
94
95
        // query whether or not, existing artefacts has to be overwritten
96 3
        if ($override === true) {
97 3
            $this->overrideArtefacts($type, $artefacts);
98
        } else {
99
            $this->appendArtefacts($type, $artefacts);
100
        }
101 3
    }
102
103
    /**
104
     * Add the passed product type artefacts to the product with the
105
     * last entity ID and overrides existing ones with the same key.
106
     *
107
     * @param string $type      The artefact type, e. g. configurable
108
     * @param array  $artefacts The product type artefacts
109
     *
110
     * @return void
111
     */
112 3
    protected function overrideArtefacts($type, array $artefacts)
113
    {
114 3
        foreach ($artefacts as $key => $artefact) {
115 3
            $this->artefacs[$type][$this->getLastEntityId()][$key] = $artefact;
116
        }
117 3
    }
118
119
    /**
120
     * Append's the passed product type artefacts to the product with the
121
     * last entity ID.
122
     *
123
     * @param string $type      The artefact type, e. g. configurable
124
     * @param array  $artefacts The product type artefacts
125
     *
126
     * @return void
127
     */
128
    protected function appendArtefacts($type, array $artefacts)
129
    {
130
        foreach ($artefacts as $artefact) {
131
            $this->artefacs[$type][$this->getLastEntityId()][] = $artefact;
132
        }
133
    }
134
135
    /**
136
     * Return the artefacts for the passed type and entity ID.
137
     *
138
     * @param string $type     The artefact type, e. g. configurable
139
     * @param string $entityId The entity ID to return the artefacts for
140
     *
141
     * @return array The array with the artefacts
142
     * @throws \Exception Is thrown, if no artefacts are available
143
     */
144 2
    public function getArtefactsByTypeAndEntityId($type, $entityId)
145
    {
146
147
        // query whether or not, artefacts for the passed params are available
148 2
        if (isset($this->artefacs[$type][$entityId])) {
149
            // load the artefacts
150 1
            $artefacts = $this->artefacs[$type][$entityId];
151
152
            // unserialize the original data
153 1
            array_walk($artefacts, function (&$artefact) {
154 1
                if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
155
                    $artefact[ColumnKeys::ORIGINAL_DATA] = unserialize($artefact[ColumnKeys::ORIGINAL_DATA]);
156
                }
157 1
            });
158
159
            // return the artefacts
160 1
            return $artefacts;
161
        }
162
163
        // throw an exception if not
164 1
        throw new \Exception(
165 1
            sprintf(
166 1
                'Cant\'t load artefacts for type %s and entity ID %d',
167 1
                $type,
168 1
                $entityId
169
            )
170
        );
171
    }
172
173
    /**
174
     * Queries whether or not artefacts for the passed type and entity ID are available.
175
     *
176
     * @param string $type     The artefact type, e. g. configurable
177
     * @param string $entityId The entity ID to return the artefacts for
178
     *
179
     * @return boolean TRUE if artefacts are available, else FALSE
180
     */
181
    public function hasArtefactsByTypeAndEntityId($type, $entityId)
182
    {
183
        return isset($this->artefacs[$type][$entityId]);
184
    }
185
186
    /**
187
     * Create's and return's a new empty artefact entity.
188
     *
189
     * @param array $columns             The array with the column data
190
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
191
     *
192
     * @return array The new artefact entity
193
     */
194 1
    public function newArtefact(array $columns, array $originalColumnNames = array())
195
    {
196
197
        // initialize the original data and the artefact
198 1
        $artefact = array();
199 1
        $originalData = array();
200
201
        // query whether or not, we've original columns
202 1
        if (sizeof($originalColumnNames) > 0) {
203
            // prepare the original column data
204 1
            $originalData[ColumnKeys::ORIGINAL_FILENAME] = $this->getFilename();
0 ignored issues
show
Bug introduced by
It seems like getFilename() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
205 1
            $originalData[ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getLineNumber();
0 ignored issues
show
Bug introduced by
It seems like getLineNumber() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
206 1
            $originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES] =  $originalColumnNames;
207
208
            // add the original column data to the new artefact
209 1
            $artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData);
210
        }
211
212
        // merge the columns into the artefact entity and return it
213 1
        return array_merge($artefact, $columns);
214
    }
215
216
    /**
217
     * Export's the artefacts to CSV files.
218
     *
219
     * @param integer $timestamp The timestamp part of the original import file
220
     * @param string  $counter   The counter part of the origin import file
221
     *
222
     * @return void
223
     */
224 1
    public function export($timestamp, $counter)
225
    {
226 1
        $this->getExportAdapter()->export($this->getArtefacts(), $this->getTargetDir(), $timestamp, $counter);
0 ignored issues
show
Bug introduced by
It seems like getTargetDir() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
227 1
    }
228
229
    /**
230
     * Set's the exporter adapter instance.
231
     *
232
     * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance
233
     *
234
     * @return void
235
     */
236 2
    public function setExportAdapter(ExportAdapterInterface $exportAdapter)
237
    {
238 2
        $this->exportAdapter = $exportAdapter;
239 2
    }
240
241
    /**
242
     * Return's the exporter adapter instance.
243
     *
244
     * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance
245
     */
246 2
    public function getExportAdapter()
247
    {
248 2
        return $this->exportAdapter;
249
    }
250
251
    /**
252
     * Set's the ID of the product that has been created recently.
253
     *
254
     * @param string $lastEntityId The entity ID
255
     *
256
     * @return void
257
     */
258 3
    public function setLastEntityId($lastEntityId)
259
    {
260 3
        $this->lastEntityId = $lastEntityId;
261 3
    }
262
263
    /**
264
     * Return's the ID of the product that has been created recently.
265
     *
266
     * @return string The entity Id
267
     */
268 3
    public function getLastEntityId()
269
    {
270 3
        return $this->lastEntityId;
271
    }
272
}
273