ExcelSplitter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ImportBundle\Extractor;
4
5
use Cocur\Slugify\Slugify;
6
use PhpOffice\PhpSpreadsheet\IOFactory;
7
use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter;
8
9
class ExcelSplitter
10
{
11
    /** @var Slugify */
12
    private $slugify;
13
14 2
    public function __construct(Slugify $slugify)
15
    {
16 2
        $this->slugify = $slugify;
17 2
    }
18
19
    /**
20
     * @param $filePath
21
     *
22
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
23
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
24
     *
25
     * @return array
26
     */
27 1
    public function split($filePath): array
28
    {
29 1
        $files = [];
30
31 1
        $xlsFilePath = realpath($filePath);
32 1
        $xlsWorkSheet = IOFactory::load($filePath);
33
        /** @var CsvWriter $csvWriter */
34 1
        $csvWriter = IOFactory::createWriter($xlsWorkSheet, 'Csv');
35 1
        foreach ($xlsWorkSheet->getWorksheetIterator() as $sheetIndex => $workSheetTab) {
36 1
            $csvWriter->setSheetIndex($sheetIndex);
37
38 1
            $sheetName = strtolower($this->slugify->slugify($workSheetTab->getTitle()));
39 1
            $filePath = sprintf(
40 1
                '%s_%s.csv',
41 1
                pathinfo($xlsFilePath, PATHINFO_DIRNAME).'/'.pathinfo($xlsFilePath, PATHINFO_FILENAME),
42 1
                $sheetName
43
            );
44
45 1
            $csvWriter->save($filePath);
46
47 1
            $files[] = ['sheet_name' => $sheetName, 'filepath' => $filePath];
48
        }
49
50 1
        return $files;
51
    }
52
}
53