ExcelFileRepository::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Mathielen\ReportWriteEngine\Output\Excel\Persistence;
3
4
use Assert\Assertion;
5
6
class ExcelFileRepository implements ExcelRepositoryInterface
7
{
8
9
    private $directory;
10
11
    private $metadata = [];
12
13 2
    public function __construct($directory)
14
    {
15 2
        Assertion::directory($directory);
16 2
        Assertion::readable($directory);
17
18 2
        $directory .= substr($directory, -1) === '/' ? '' : '/'; //add ending slash
19 2
        $this->directory = $directory;
20 2
    }
21
22
    public function setMetadata($id, $metadata = null)
23
    {
24
        $this->metadata[$id] = $metadata;
25
    }
26
27
    public function getMetadata($id)
28
    {
29
        return isset($this->metadata[$id]) ? $this->metadata[$id] : null;
30
    }
31
32
    /**
33
     * @return \PHPExcel
34
     */
35 1
    public function get($id)
36
    {
37 1
        Assertion::notEmpty($id);
38 1
        Assertion::string($id);
39
40 1
        $filePath = $this->directory . $id;
41
42 1
        $inputFileType = \PHPExcel_IOFactory::identify($filePath);
43 1
        $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
44
45 1
        return $objReader->load($filePath);
46
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function save(\PHPExcel $excel, $id)
52
    {
53 1
        Assertion::notEmpty($id);
54 1
        Assertion::string($id);
55 1
        Assertion::writeable($this->directory);
56
57 1
        $filePath = $this->directory . $id;
58
59 1
        $pathinfo = pathinfo($id);
60 1
        if (!isset($pathinfo['extension'])) {
61
            throw new \RuntimeException("Extension was expected for report id: '$id'. Unable to determine excel type.");
62
        }
63
64 1
        $writerType = self::extensionToType($pathinfo['extension']);
65 1
        $excelWriter = \PHPExcel_IOFactory::createWriter($excel, $writerType);
66
67 1
        $excelWriter->save($filePath);
68
69 1
        return $filePath;
70
    }
71
72 1
    private static function extensionToType($extension)
73
    {
74 1
        $extensionType = null;
75
76 1
        switch (strtolower($extension)) {
77 1
            case 'xlsx':            //	Excel (OfficeOpenXML) Spreadsheet
78 1
            case 'xlsm':            //	Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
79 1
            case 'xltx':            //	Excel (OfficeOpenXML) Template
80 1
            case 'xltm':            //	Excel (OfficeOpenXML) Macro Template (macros will be discarded)
81 1
                $extensionType = 'Excel2007';
82 1
                break;
83
            case 'xls':                //	Excel (BIFF) Spreadsheet
84
            case 'xlt':                //	Excel (BIFF) Template
85
                $extensionType = 'Excel5';
86
                break;
87
            case 'ods':                //	Open/Libre Offic Calc
88
            case 'ots':                //	Open/Libre Offic Calc Template
89
                $extensionType = 'OOCalc';
90
                break;
91
            case 'slk':
92
                $extensionType = 'SYLK';
93
                break;
94
            case 'xml':                //	Excel 2003 SpreadSheetML
95
                $extensionType = 'Excel2003XML';
96
                break;
97
            case 'gnumeric':
98
                $extensionType = 'Gnumeric';
99
                break;
100
            case 'htm':
101
            case 'html':
102
                $extensionType = 'HTML';
103
                break;
104
            case 'csv':
105
                $extensionType = 'CSV';
106
                break;
107 1
        }
108
109 1
        return $extensionType;
110
    }
111
}
112