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

ExporterToExcel::toFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 10
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