Completed
Push — master ( 79fd8a...81e819 )
by Christian
05:05
created

ExporterToExcel   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 8
cp 0
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 3 1
A toFile() 0 10 1
1
<?php
2
3
namespace Omatech\Mage\Core\Adapters\Translations\Exporters;
4
5
use Carbon\Carbon;
6
use Rap2hpoutre\FastExcel\FastExcel;
7
use Rap2hpoutre\FastExcel\SheetCollection;
8
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface;
9
10
class ExporterToExcel implements ExportTranslationInterface
11
{
12
    /**
13
     * @param array $translations
14
     * @return string
15
     * @throws \Box\Spout\Common\Exception\IOException
16
     * @throws \Box\Spout\Common\Exception\InvalidArgumentException
17
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
18
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
19
     */
20
    public function export(array $translations): string
21
    {
22
        return $this->toFile($translations);
23
    }
24
25
    /**
26
     * @param $translations
27
     * @return string
28
     * @throws \Box\Spout\Common\Exception\IOException
29
     * @throws \Box\Spout\Common\Exception\InvalidArgumentException
30
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
31
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
32
     */
33
    private function toFile($translations): string
34
    {
35
        $date = Carbon::now('Europe/Madrid')->format('dmY_His');
36
        $path = storage_path('app/translations/' . $date . '_excel.xlsx');
37
38
        return (new FastExcel(new SheetCollection($translations)))->export($path, function ($sheets) {
39
            return [
40
                'key1' => 'mage',
41
                'key2' => $sheets['key'],
42
                'value' => $sheets['value']
43
            ];
44
        });
45
    }
46
}
47