ExcelSplitter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A split() 0 24 2
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