Completed
Pull Request — master (#89)
by Tim
06:59
created

ExportableTrait::export()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
     * Return's the artefacts for post-processing.
54
     *
55
     * @return array The artefacts
56
     */
57 3
    public function getArtefacts()
58
    {
59 3
        return $this->artefacs;
60
    }
61
62
    /**
63
     * Add the passed product type artefacts to the product with the
64
     * last entity ID.
65
     *
66
     * @param string $type      The artefact type, e. g. configurable
67
     * @param array  $artefacts The product type artefacts
68
     *
69
     * @return void
70
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
71
     */
72 4
    public function addArtefacts($type, array $artefacts)
73
    {
74
75
        // query whether or not, any artefacts are available
76 4
        if (sizeof($artefacts) === 0) {
77 1
            return;
78
        }
79
80
        // serialize the original data
81 3
        array_walk($artefacts, function(&$artefact) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
82 3
            if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
83 1
                $artefact[ColumnKeys::ORIGINAL_DATA] = serialize($artefact[ColumnKeys::ORIGINAL_DATA]);
84
            }
85 3
        });
86
87
        // append the artefacts to the stack
88 3
        $this->artefacs[$type][$this->getLastEntityId()][] = $artefacts;
0 ignored issues
show
Bug introduced by
It seems like getLastEntityId() 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...
89 3
    }
90
91
    /**
92
     * Return the artefacts for the passed type and entity ID.
93
     *
94
     * @param string $type     The artefact type, e. g. configurable
95
     * @param string $entityId The entity ID to return the artefacts for
96
     *
97
     * @return array The array with the artefacts
98
     * @throws \Exception Is thrown, if no artefacts are available
99
     */
100 2
    public function getArtefactsByTypeAndEntityId($type, $entityId)
101
    {
102
103
        // query whether or not, artefacts for the passed params are available
104 2
        if (isset($this->artefacs[$type][$entityId])) {
105 1
            return reset($this->artefacs[$type][$entityId]);
106
        }
107
108
        // throw an exception if not
109 1
        throw new \Exception(
110 1
            sprintf(
111 1
                'Cant\'t load artefacts for type %s and entity ID %d',
112 1
                $type,
113 1
                $entityId
114
            )
115
        );
116
    }
117
118
    /**
119
     * Create's and return's a new empty artefact entity.
120
     *
121
     * @param array $columns             The array with the column data
122
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
123
     *
124
     * @return array The new artefact entity
125
     */
126 1
    public function newArtefact(array $columns, array $originalColumnNames = array())
127
    {
128
129
        // initialize the original data
130
        $originalData = array(
131 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...
132 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...
133 1
            ColumnKeys::ORIGINAL_COLUMN_NAMES => $originalColumnNames
134
        );
135
136
        // prepare a new artefact entity
137 1
        $artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData);
138
139
        // merge the columns into the artefact entity and return it
140 1
        return array_merge($artefact, $columns);
141
    }
142
143
    /**
144
     * Export's the artefacts to CSV files.
145
     *
146
     * @param integer $timestamp The timestamp part of the original import file
147
     * @param string  $counter   The counter part of the origin import file
148
     *
149
     * @return void
150
     */
151 1
    public function export($timestamp, $counter)
152
    {
153 1
        $this->getExportAdapter()->export($this->getArtefacts(), $this->getTargetDir(), $timestamp, $counter);
154 1
    }
155
156
    /**
157
     * Set's the exporter adapter instance.
158
     *
159
     * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance
160
     *
161
     * @return void
162
     */
163 2
    public function setExportAdapter(ExportAdapterInterface $exportAdapter)
164
    {
165 2
        $this->exportAdapter = $exportAdapter;
166 2
    }
167
168
    /**
169
     * Return's the exporter adapter instance.
170
     *
171
     * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance
172
     */
173 2
    public function getExportAdapter()
174
    {
175 2
        return $this->exportAdapter;
176
    }
177
178
    /**
179
     * Return's the target directory for the artefact export.
180
     *
181
     * @return string The target directory for the artefact export
182
     */
183 1
    protected function getTargetDir()
184
    {
185 1
        return $this->getNewSourceDir($this->getSerial());
0 ignored issues
show
Bug introduced by
It seems like getSerial() 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...
Bug introduced by
It seems like getNewSourceDir() 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...
186
    }
187
}
188