TranslationsExporter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 5
c 5
b 0
f 0
lcom 2
cbo 3
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A export() 0 11 1
A formatData() 0 12 2
A getFileName() 0 4 1
1
<?php namespace Modules\Translation\Exporters;
2
3
use League\Csv\Writer;
4
use Modules\Translation\Services\TranslationsService;
5
use SplTempFileObject;
6
7
class TranslationsExporter
8
{
9
    /**
10
     * @var TranslationsService
11
     */
12
    private $translations;
13
    private $filename = 'translations_';
14
15
    public function __construct(TranslationsService $translations)
16
    {
17
        $this->translations = $translations;
18
    }
19
20
    public function export()
21
    {
22
        $data = $this->formatData();
23
        $keys = array_keys($data[0]);
24
25
        $csv = Writer::createFromFileObject(new SplTempFileObject());
26
        $csv->insertOne($keys);
27
        $csv->insertAll($data);
28
29
        return (string) $csv;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getFileName()
36
    {
37
        return $this->filename . time() . '.csv';
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    private function formatData()
44
    {
45
        $translations = $this->translations->getFileAndDatabaseMergedTranslations();
46
        $translations = $translations->all();
47
48
        $data = [];
49
        foreach ($translations as $key => $translation) {
50
            $data[] = array_merge(['key' => $key], $translation);
51
        }
52
53
        return $data;
54
    }
55
}
56