Spreadsheet::getStyles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Akeneo\Component\SpreadsheetParser\Xlsx;
4
5
use Akeneo\Component\SpreadsheetParser\SpreadsheetInterface;
6
7
/**
8
 * Represents an XLSX spreadsheet
9
 *
10
 * @author    Antoine Guigan <[email protected]>
11
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
12
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
13
 */
14
class Spreadsheet implements SpreadsheetInterface
15
{
16
17
    /**
18
     * @staticvar string Path to the relationships file inside the XLSX archive
19
     */
20
    const RELATIONSHIPS_PATH = 'xl/_rels/workbook.xml.rels';
21
22
    /**
23
     * @staticvar string Path to the spreadsheets file inside the XLSX archive
24
     */
25
    const WORKBOOK_PATH = 'xl/workbook.xml';
26
27
    /**
28
     * @var RelationshipsLoader
29
     */
30
    protected $relationshipsLoader;
31
32
    /**
33
     * @var ValueTransformerFactory
34
     */
35
    protected $valueTransformerFactory;
36
37
    /**
38
     *
39
     * @var SharedStringsLoader
40
     */
41
    protected $sharedStringsLoader;
42
43
    /**
44
     *
45
     * @var RowIteratorFactory
46
     */
47
    protected $rowIteratorFactory;
48
49
    /**
50
     * @var Archive
51
     */
52
    protected $archive;
53
54
    /**
55
     * @var StylesLoader
56
     */
57
    protected $stylesLoader;
58
59
    /**
60
     * @var WorksheetListReader
61
     */
62
    protected $worksheetListReader;
63
64
    /**
65
     * @var Relationships
66
     */
67
    private $relationships;
68
69
    /**
70
     * @var ValueTransformer
71
     */
72
    private $valueTransformer;
73
74
    /**
75
     * @var SharedStrings
76
     */
77
    private $sharedStrings;
78
79
    /**
80
     * @var array
81
     */
82
    private $worksheetPaths;
83
84
    /**
85
     * @var Styles
86
     */
87
    private $styles;
88
89
    /**
90
     * Constructor
91
     *
92
     * @param Archive                 $archive
93
     * @param RelationshipsLoader     $relationshipsLoader
94
     * @param SharedStringsLoader     $sharedStringsLoader
95
     * @param StylesLoader            $stylesLoader
96
     * @param WorksheetListReader     $worksheetListReader
97
     * @param ValueTransformerFactory $valueTransformerFactory
98
     * @param RowIteratorFactory      $rowIteratorFactory
99
     */
100 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
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...
101
        Archive $archive,
102
        RelationshipsLoader $relationshipsLoader,
103
        SharedStringsLoader $sharedStringsLoader,
104
        StylesLoader $stylesLoader,
105
        WorksheetListReader $worksheetListReader,
106
        ValueTransformerFactory $valueTransformerFactory,
107
        RowIteratorFactory $rowIteratorFactory
108
    ) {
109
        $this->archive = $archive;
110
        $this->relationshipsLoader = $relationshipsLoader;
111
        $this->sharedStringsLoader = $sharedStringsLoader;
112
        $this->stylesLoader = $stylesLoader;
113
        $this->worksheetListReader = $worksheetListReader;
114
        $this->valueTransformerFactory = $valueTransformerFactory;
115
        $this->rowIteratorFactory = $rowIteratorFactory;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getWorksheets()
122
    {
123
        return array_keys($this->getWorksheetPaths());
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function createRowIterator($worksheetIndex, array $options = [])
130
    {
131
        $paths = array_values($this->getWorksheetPaths());
132
133
        return $this->rowIteratorFactory->create(
134
            $this->getValueTransformer(),
135
            $this->archive->extract($paths[$worksheetIndex]),
136
            $options,
137
            $this->archive
138
        );
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getWorksheetIndex($name)
145
    {
146
        return array_search($name, $this->getWorksheets());
147
    }
148
149
    /**
150
     * @return Relationships
151
     */
152
    protected function getRelationships()
153
    {
154
        if (!$this->relationships) {
155
            $path = $this->archive->extract(static::RELATIONSHIPS_PATH);
156
            $this->relationships = $this->relationshipsLoader->open($path);
157
        }
158
159
        return $this->relationships;
160
    }
161
162
    /**
163
     * @return ValueTransformer
164
     */
165
    protected function getValueTransformer()
166
    {
167
        if (!$this->valueTransformer) {
168
            $this->valueTransformer = $this->valueTransformerFactory->create(
169
                $this->getSharedStrings(),
170
                $this->getStyles()
171
            );
172
        }
173
174
        return $this->valueTransformer;
175
    }
176
177
    /**
178
     * @return SharedStrings
179
     */
180
    protected function getSharedStrings()
181
    {
182
        if (!$this->sharedStrings) {
183
            $path = $this->archive->extract($this->relationships->getSharedStringsPath());
184
            $this->sharedStrings = $this->sharedStringsLoader->open($path, $this->archive);
185
        }
186
187
        return $this->sharedStrings;
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    protected function getWorksheetPaths()
194
    {
195
        if (!$this->worksheetPaths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->worksheetPaths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
196
            $path = $this->archive->extract(static::WORKBOOK_PATH);
197
            $this->worksheetPaths = $this->worksheetListReader->getWorksheetPaths($this->getRelationships(), $path);
198
        }
199
200
        return $this->worksheetPaths;
201
    }
202
203
    /**
204
     * @return Styles
205
     */
206
    protected function getStyles()
207
    {
208
        if (!$this->styles) {
209
            $path = $this->archive->extract($this->relationships->getStylesPath());
210
            $this->styles = $this->stylesLoader->open($path, $this->archive);
211
        }
212
213
        return $this->styles;
214
    }
215
}
216