Completed
Push — master ( aa9c55...6ca603 )
by Tim
8s
created

ExportableTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 137
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2
ccs 0
cts 57
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArtefacts() 0 4 1
A addArtefacts() 0 11 2
B export() 0 31 5
A getTargetDir() 0 4 1
C getExportConfig() 0 39 7
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 Goodby\CSV\Export\Standard\Exporter;
24
use Goodby\CSV\Export\Standard\ExporterConfig;
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
     * Return's the artefacts for post-processing.
47
     *
48
     * @return array The artefacts
49
     */
50
    public function getArtefacts()
51
    {
52
        return $this->artefacs;
53
    }
54
55
    /**
56
     * Add the passed product type artefacts to the product with the
57
     * last entity ID.
58
     *
59
     * @param string $type      The artefact type, e. g. configurable
60
     * @param array  $artefacts The product type artefacts
61
     *
62
     * @return void
63
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
64
     */
65
    public function addArtefacts($type, array $artefacts)
66
    {
67
68
        // query whether or not, any artefacts are available
69
        if (sizeof($artefacts) === 0) {
70
            return;
71
        }
72
73
        // append the artefacts to the stack
74
        $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...
75
    }
76
77
    /**
78
     * Export's the artefacts to CSV files.
79
     *
80
     * @param integer $timestamp The timestamp part of the original import file
81
     * @param string  $counter   The counter part of the origin import file
82
     *
83
     * @return void
84
     */
85
    public function export($timestamp, $counter)
86
    {
87
88
        // load the target directory and the actual timestamp
89
        $targetDir = $this->getTargetDir();
90
91
        // iterate over the artefacts and export them
92
        foreach ($this->getArtefacts() as $artefactType => $artefacts) {
93
            // initialize the bunch and the exporter
94
            $bunch = array();
95
            $exporter = new Exporter($this->getExportConfig());
96
97
            // iterate over the artefact types artefacts
98
            foreach ($artefacts as $entityArtefacts) {
99
                // set the bunch header and append the artefact data
100
                if (sizeof($bunch) === 0) {
101
                    $first = reset($entityArtefacts);
102
                    $second = reset($first);
103
                    $bunch[] = array_keys($second);
104
                }
105
106
                // export the artefacts
107
                foreach ($entityArtefacts as $entityArtefact) {
108
                    $bunch = array_merge($bunch, $entityArtefact);
109
                }
110
            }
111
112
            // export the artefact (bunch)
113
            $exporter->export(sprintf('%s/%s_%s_%s.csv', $targetDir, $artefactType, $timestamp, $counter), $bunch);
114
        }
115
    }
116
117
    /**
118
     * Return's the target directory for the artefact export.
119
     *
120
     * @return string The target directory for the artefact export
121
     */
122
    protected function getTargetDir()
123
    {
124
        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...
125
    }
126
127
    /**
128
     * Initialize and return the exporter configuration.
129
     *
130
     * @return \Goodby\CSV\Export\Standard\ExporterConfig The exporter configuration
131
     */
132
    protected function getExportConfig()
133
    {
134
135
        // initialize the lexer configuration
136
        $config = new ExporterConfig();
137
138
        // query whether or not a delimiter character has been configured
139
        if ($delimiter = $this->getConfiguration()->getDelimiter()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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
            $config->setDelimiter($delimiter);
141
        }
142
143
        // query whether or not a custom escape character has been configured
144
        if ($escape = $this->getConfiguration()->getEscape()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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...
145
            $config->setEscape($escape);
146
        }
147
148
        // query whether or not a custom enclosure character has been configured
149
        if ($enclosure = $this->getConfiguration()->getEnclosure()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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...
150
            $config->setEnclosure($enclosure);
151
        }
152
153
        // query whether or not a custom source charset has been configured
154
        if ($fromCharset = $this->getConfiguration()->getFromCharset()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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...
155
            $config->setFromCharset($fromCharset);
156
        }
157
158
        // query whether or not a custom target charset has been configured
159
        if ($toCharset = $this->getConfiguration()->getToCharset()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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...
160
            $config->setToCharset($toCharset);
161
        }
162
163
        // query whether or not a custom file mode has been configured
164
        if ($fileMode = $this->getConfiguration()->getFileMode()) {
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() 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...
165
            $config->setFileMode($fileMode);
166
        }
167
168
        // return the lexer configuratio
169
        return $config;
170
    }
171
}
172