Completed
Push — master ( a98764...852cbb )
by diego
25s
created

Classes/Service/Resources/Excel.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace HDNET\Importr\Service\Resources;
3
4
use HDNET\Importr\Domain\Model\Strategy;
5
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
6
use TYPO3\CMS\Core\Utility\GeneralUtility;
7
8
/**
9
 * Description of Excel
10
 *
11
 * @author timlochmueller
12
 */
13
class Excel extends AbstractResource implements ResourceInterface
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $filepathExpression = "/.xlsx?$/";
20
21
    /**
22
     * @var array
23
     */
24
    protected $content = [];
25
26
    /**
27
     * @var string
28
     */
29
    protected $filepath;
30
31
    /**
32
     * @return mixed
33
     */
34 View Code Duplication
    public function getConfiguration()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $configuration = parent::getConfiguration();
37
        $configuration['skipRows'] = isset($configuration['skipRows']) ? (int)$configuration['skipRows'] : 0;
38
        $configuration['sheet'] = isset($configuration['sheet']) ? (int)$configuration['sheet'] : -1;
39
        return $configuration;
40
    }
41
42
    /**
43
     * @param Strategy $strategy
44
     * @param string   $filepath
45
     */
46
    public function start(Strategy $strategy, $filepath)
47
    {
48
        $this->filepath = $filepath;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getFilepathExpression()
55
    {
56
        return $this->filepathExpression;
57
    }
58
59
    /**
60
     *
61
     */
62
    public function parseResource()
63
    {
64
        $configuration = $this->getConfiguration();
65
66
        if (!ExtensionManagementUtility::isLoaded('phpexcel_library')) {
67
            throw new \Exception('phpexcel_library is not loaded', 12367812368);
68
        }
69
70
        $filename = GeneralUtility::getFileAbsFileName($this->filepath);
71
        GeneralUtility::makeInstanceService('phpexcel');
72
73
        $objReader = \PHPExcel_IOFactory::createReaderForFile($filename);
74
        $objReader->setReadDataOnly(true);
75
        $objPHPExcel = $objReader->load($filename);
76
        if ($configuration['sheet'] >= 0) {
77
            $objWorksheet = $objPHPExcel->getSheet($configuration['sheet']);
78
        } else {
79
            $objWorksheet = $objPHPExcel->getActiveSheet();
80
        }
81
82
        $highestRow = $objWorksheet->getHighestRow();
83
        $highestColumn = $objWorksheet->getHighestColumn();
84
85
        $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
86
87
        for ($row = 1 + $configuration['skipRows']; $row <= $highestRow; ++$row) {
88
            $rowRecord = [];
89
            for ($col = 0; $col <= $highestColumnIndex; ++$col) {
90
                $rowRecord[] = trim(
91
                    $objWorksheet->getCellByColumnAndRow($col, $row)
92
                        ->getValue()
93
                );
94
            }
95
            $this->content[] = $rowRecord;
96
        }
97
    }
98
99
    /**
100
     * @return integer
101
     */
102
    public function getAmount()
103
    {
104
        return count($this->content);
105
    }
106
107
    /**
108
     * @param integer $pointer
109
     *
110
     * @return mixed
111
     */
112
    public function getEntry($pointer)
113
    {
114
        return $this->content[$pointer];
115
    }
116
117
    /**
118
     *
119
     */
120
    public function end()
121
    {
122
    }
123
}
124