Completed
Push — master ( a3f369...f825b4 )
by Tim
14s
created

CsvExportAdapter::export()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 26
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 6
nop 4
crap 30
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\CsvExportAdapter
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\Adapter;
22
23
use Goodby\CSV\Export\Protocol\ExporterInterface;
24
25
/**
26
 * CSV export adapter implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class CsvExportAdapter implements ExportAdapterInterface
35
{
36
37
    /**
38
     * The exporter instance.
39
     *
40
     * @var \Goodby\CSV\Export\Protocol\ExporterInterface
41
     */
42
    protected $exporter;
43
44
    /**
45
     * Initialize the adapter with the configuration.
46
     *
47
     * @param \Goodby\CSV\Export\Protocol\ExporterInterface $exporter The exporter instance
48
     */
49
    public function __construct(ExporterInterface $exporter)
50
    {
51
        $this->exporter = $exporter;
52
    }
53
54
    /**
55
     * Imports the content of the CSV file with the passed filename.
56
     *
57
     * @param array   $artefacts The artefacts to be exported
58
     * @param string  $targetDir The target dir to export the artefacts to
59
     * @param integer $timestamp The timestamp part of the original import file
60
     * @param string  $counter   The counter part of the origin import file
61
     *
62
     * @return void
63
     */
64
    public function export(array $artefacts, $targetDir, $timestamp, $counter)
65
    {
66
67
        // iterate over the artefacts and export them
68
        foreach ($artefacts as $artefactType => $artefacts) {
69
            // initialize the bunch and the exporter
70
            $bunch = array();
71
72
            // iterate over the artefact types artefacts
73
            foreach ($artefacts as $entityArtefacts) {
74
                // set the bunch header and append the artefact data
75
                if (sizeof($bunch) === 0) {
76
                    $first = reset($entityArtefacts);
77
                    $second = reset($first);
78
                    $bunch[] = array_keys($second);
79
                }
80
81
                // export the artefacts
82
                foreach ($entityArtefacts as $entityArtefact) {
83
                    $bunch = array_merge($bunch, $entityArtefact);
84
                }
85
            }
86
87
            // export the artefact (bunch)
88
            $this->exporter->export(
89
                sprintf(
90
                    '%s/%s_%s_%s.csv',
91
                    $targetDir,
92
                    $artefactType,
93
                    $timestamp,
94
                    $counter
95
                ),
96
                $bunch
97
            );
98
        }
99
    }
100
}
101