Completed
Pull Request — master (#51)
by Tim
09:44
created

ExportableTrait::getExportConfig()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 6.7272
cc 7
eloc 15
nc 64
nop 0
crap 56
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\Utils\ExporterTrait;
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 exporter trait implementation.
40
     *
41
     * @var \TechDivision\Import\Utils\ExporterTrait
42
     */
43
    use ExporterTrait;
44
45
    /**
46
     * The array containing the data for product type configuration (configurables, bundles, etc).
47
     *
48
     * @var array
49
     */
50
    protected $artefacs = array();
51
52
    /**
53
     * Return's the artefacts for post-processing.
54
     *
55
     * @return array The artefacts
56
     */
57
    public function getArtefacts()
58
    {
59
        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
    public function addArtefacts($type, array $artefacts)
73
    {
74
75
        // query whether or not, any artefacts are available
76
        if (sizeof($artefacts) === 0) {
77
            return;
78
        }
79
80
        // serialize the original data
81
        array_walk($artefacts, function(&$artefact) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
82
            if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) {
83
                $artefact[ColumnKeys::ORIGINAL_DATA] = serialize($artefact[ColumnKeys::ORIGINAL_DATA]);
84
            }
85
        });
86
87
        // append the artefacts to the stack
88
        $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
    }
90
91
    /**
92
     * Create's and return's a new empty artefact entity.
93
     *
94
     * @param array $columns             The array with the column data
95
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
96
     *
97
     * @return array The new artefact entity
98
     */
99
    public function newArtefact(array $columns, array $originalColumnNames = array())
100
    {
101
102
        // initialize the original data
103
        $originalData = array(
104
            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...
105
            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...
106
            ColumnKeys::ORIGINAL_COLUMN_NAMES => $originalColumnNames
107
        );
108
109
        // prepare a new artefact entity
110
        $artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData);
111
112
        // merge the columns into the artefact entity and return it
113
        return array_merge($artefact, $columns);
114
    }
115
116
    /**
117
     * Export's the artefacts to CSV files.
118
     *
119
     * @param integer $timestamp The timestamp part of the original import file
120
     * @param string  $counter   The counter part of the origin import file
121
     *
122
     * @return void
123
     */
124
    public function export($timestamp, $counter)
125
    {
126
127
        // load the target directory and the actual timestamp
128
        $targetDir = $this->getTargetDir();
129
130
        // iterate over the artefacts and export them
131
        foreach ($this->getArtefacts() as $artefactType => $artefacts) {
132
            // initialize the bunch and the exporter
133
            $bunch = array();
134
135
            // iterate over the artefact types artefacts
136
            foreach ($artefacts as $entityArtefacts) {
137
                // set the bunch header and append the artefact data
138
                if (sizeof($bunch) === 0) {
139
                    $first = reset($entityArtefacts);
140
                    $second = reset($first);
141
                    $bunch[] = array_keys($second);
142
                }
143
144
                // export the artefacts
145
                foreach ($entityArtefacts as $entityArtefact) {
146
                    $bunch = array_merge($bunch, $entityArtefact);
147
                }
148
            }
149
150
            // export the artefact (bunch)
151
            $this->getExporterInstance()->export(
152
                sprintf(
153
                    '%s/%s_%s_%s.csv',
154
                    $targetDir,
155
                    $artefactType,
156
                    $timestamp,
157
                    $counter
158
                ),
159
                $bunch
160
            );
161
        }
162
    }
163
164
    /**
165
     * Return's the target directory for the artefact export.
166
     *
167
     * @return string The target directory for the artefact export
168
     */
169
    protected function getTargetDir()
170
    {
171
        return $this->getNewSourceDir();
0 ignored issues
show
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...
172
    }
173
}
174