Completed
Push — master ( 3eced2...c3d040 )
by Tim
15s
created

ExportableTrait::getArtefacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     *
76
     * @return void
77
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
78
     */
79 4
    public function addArtefacts($type, array $artefacts)
80
    {
81
82
        // query whether or not, any artefacts are available
83 4
        if (sizeof($artefacts) === 0) {
84 1
            return;
85
        }
86
87
        // serialize the original data
88
        array_walk($artefacts, function(&$artefact) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
89 3
            if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
90 1
                $artefact[ColumnKeys::ORIGINAL_DATA] = serialize($artefact[ColumnKeys::ORIGINAL_DATA]);
91
            }
92 3
        });
93
94
        // initialize the artefacts for the passed type and entity ID
95 3
        if (!isset($this->artefacs[$type][$this->getLastEntityId()])) {
96 3
            $this->artefacs[$type][$this->getLastEntityId()] = array();
97
        }
98
99
        // append the artefacts to the stack
100 3
        foreach ($artefacts as $key => $artefact) {
101 3
            $this->artefacs[$type][$this->getLastEntityId()][$key] = $artefact;
102
        }
103 3
    }
104
105
    /**
106
     * Return the artefacts for the passed type and entity ID.
107
     *
108
     * @param string $type     The artefact type, e. g. configurable
109
     * @param string $entityId The entity ID to return the artefacts for
110
     *
111
     * @return array The array with the artefacts
112
     * @throws \Exception Is thrown, if no artefacts are available
113
     */
114 2
    public function getArtefactsByTypeAndEntityId($type, $entityId)
115
    {
116
117
        // query whether or not, artefacts for the passed params are available
118 2
        if (isset($this->artefacs[$type][$entityId])) {
119
            // load the artefacts
120 1
            $artefacts = $this->artefacs[$type][$entityId];
121
122
            // unserialize the original data
123 1
            array_walk($artefacts, function(&$artefact) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
124 1
                if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
125
                    $artefact[ColumnKeys::ORIGINAL_DATA] = unserialize($artefact[ColumnKeys::ORIGINAL_DATA]);
126
                }
127 1
            });
128
129
            // return the artefacts
130 1
            return $artefacts;
131
        }
132
133
        // throw an exception if not
134 1
        throw new \Exception(
135 1
            sprintf(
136 1
                'Cant\'t load artefacts for type %s and entity ID %d',
137 1
                $type,
138 1
                $entityId
139
            )
140
        );
141
    }
142
143
    /**
144
     * Queries whether or not artefacts for the passed type and entity ID are available.
145
     *
146
     * @param string $type     The artefact type, e. g. configurable
147
     * @param string $entityId The entity ID to return the artefacts for
148
     *
149
     * @return boolean TRUE if artefacts are available, else FALSE
150
     */
151
    public function hasArtefactsByTypeAndEntityId($type, $entityId)
152
    {
153
        return isset($this->artefacs[$type][$entityId]);
154
    }
155
156
    /**
157
     * Create's and return's a new empty artefact entity.
158
     *
159
     * @param array $columns             The array with the column data
160
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
161
     *
162
     * @return array The new artefact entity
163
     */
164 1
    public function newArtefact(array $columns, array $originalColumnNames = array())
165
    {
166
167
        // initialize the original data
168 1
        $originalData = array();
169 1
        if (sizeof($originalColumnNames) > 0) {
170 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...
171 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...
172 1
            $originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES] =  $originalColumnNames;
173
        }
174
175
        // prepare a new artefact entity
176 1
        $artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData);
177
178
        // merge the columns into the artefact entity and return it
179 1
        return array_merge($artefact, $columns);
180
    }
181
182
    /**
183
     * Export's the artefacts to CSV files.
184
     *
185
     * @param integer $timestamp The timestamp part of the original import file
186
     * @param string  $counter   The counter part of the origin import file
187
     *
188
     * @return void
189
     */
190 1
    public function export($timestamp, $counter)
191
    {
192 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...
193 1
    }
194
195
    /**
196
     * Set's the exporter adapter instance.
197
     *
198
     * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance
199
     *
200
     * @return void
201
     */
202 2
    public function setExportAdapter(ExportAdapterInterface $exportAdapter)
203
    {
204 2
        $this->exportAdapter = $exportAdapter;
205 2
    }
206
207
    /**
208
     * Return's the exporter adapter instance.
209
     *
210
     * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance
211
     */
212 2
    public function getExportAdapter()
213
    {
214 2
        return $this->exportAdapter;
215
    }
216
217
    /**
218
     * Set's the ID of the product that has been created recently.
219
     *
220
     * @param string $lastEntityId The entity ID
221
     *
222
     * @return void
223
     */
224 3
    public function setLastEntityId($lastEntityId)
225
    {
226 3
        $this->lastEntityId = $lastEntityId;
227 3
    }
228
229
    /**
230
     * Return's the ID of the product that has been created recently.
231
     *
232
     * @return string The entity Id
233
     */
234 3
    public function getLastEntityId()
235
    {
236 3
        return $this->lastEntityId;
237
    }
238
}
239