Completed
Pull Request — master (#89)
by Tim
02:19
created

ExportableTrait::newArtefact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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 3
        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
        // append the artefacts to the stack
95 3
        $this->artefacs[$type][$this->getLastEntityId()][] = $artefacts;
96 3
    }
97
98
    /**
99
     * Return the artefacts for the passed type and entity ID.
100
     *
101
     * @param string $type     The artefact type, e. g. configurable
102
     * @param string $entityId The entity ID to return the artefacts for
103
     *
104
     * @return array The array with the artefacts
105
     * @throws \Exception Is thrown, if no artefacts are available
106
     */
107 2
    public function getArtefactsByTypeAndEntityId($type, $entityId)
108
    {
109
110
        // query whether or not, artefacts for the passed params are available
111 2
        if (isset($this->artefacs[$type][$entityId])) {
112 1
            return reset($this->artefacs[$type][$entityId]);
113
        }
114
115
        // throw an exception if not
116 1
        throw new \Exception(
117 1
            sprintf(
118 1
                'Cant\'t load artefacts for type %s and entity ID %d',
119 1
                $type,
120 1
                $entityId
121
            )
122
        );
123
    }
124
125
    /**
126
     * Create's and return's a new empty artefact entity.
127
     *
128
     * @param array $columns             The array with the column data
129
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
130
     *
131
     * @return array The new artefact entity
132
     */
133 1
    public function newArtefact(array $columns, array $originalColumnNames = array())
134
    {
135
136
        // initialize the original data
137
        $originalData = array(
138 1
            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...
139 1
            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...
140 1
            ColumnKeys::ORIGINAL_COLUMN_NAMES => $originalColumnNames
141
        );
142
143
        // prepare a new artefact entity
144 1
        $artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData);
145
146
        // merge the columns into the artefact entity and return it
147 1
        return array_merge($artefact, $columns);
148
    }
149
150
    /**
151
     * Export's the artefacts to CSV files.
152
     *
153
     * @param integer $timestamp The timestamp part of the original import file
154
     * @param string  $counter   The counter part of the origin import file
155
     *
156
     * @return void
157
     */
158 1
    public function export($timestamp, $counter)
159
    {
160 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...
161 1
    }
162
163
    /**
164
     * Set's the exporter adapter instance.
165
     *
166
     * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance
167
     *
168
     * @return void
169
     */
170 2
    public function setExportAdapter(ExportAdapterInterface $exportAdapter)
171
    {
172 2
        $this->exportAdapter = $exportAdapter;
173 2
    }
174
175
    /**
176
     * Return's the exporter adapter instance.
177
     *
178
     * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance
179
     */
180 2
    public function getExportAdapter()
181
    {
182 2
        return $this->exportAdapter;
183
    }
184
185
    /**
186
     * Set's the ID of the product that has been created recently.
187
     *
188
     * @param string $lastEntityId The entity ID
189
     *
190
     * @return void
191
     */
192 3
    public function setLastEntityId($lastEntityId)
193
    {
194 3
        $this->lastEntityId = $lastEntityId;
195 3
    }
196
197
    /**
198
     * Return's the ID of the product that has been created recently.
199
     *
200
     * @return string The entity Id
201
     */
202 3
    public function getLastEntityId()
203
    {
204 3
        return $this->lastEntityId;
205
    }
206
}
207