Completed
Pull Request — master (#557)
by Adrien
03:10
created

SettingsHelper::getActiveSheetName()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
rs 8.5125
c 1
b 0
f 0
cc 5
eloc 13
nc 8
nop 1
crap 5.0144
1
<?php
2
3
namespace Box\Spout\Reader\ODS\Helper;
4
5
use Box\Spout\Reader\Exception\XMLProcessingException;
6
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory;
7
8
/**
9
 * Class SettingsHelper
10
 * This class provides helper functions to extract data from the "settings.xml" file.
11
 */
12
class SettingsHelper
13
{
14
    const SETTINGS_XML_FILE_PATH = 'settings.xml';
15
16
    /** Definition of XML nodes name and attribute used to parse settings data */
17
    const XML_NODE_CONFIG_ITEM = 'config:config-item';
18
    const XML_ATTRIBUTE_CONFIG_NAME = 'config:name';
19
    const XML_ATTRIBUTE_VALUE_ACTIVE_TABLE = 'ActiveTable';
20
21
    /** @var InternalEntityFactory Factory to create entities */
22
    private $entityFactory;
23
24
    /**
25
     * @param InternalEntityFactory $entityFactory Factory to create entities
26
     */
27 31
    public function __construct($entityFactory)
28
    {
29 31
        $this->entityFactory = $entityFactory;
30 31
    }
31
32
    /**
33
     * @param string $filePath Path of the file to be read
34
     * @return string|null Name of the sheet that was defined as active or NULL if none found
35
     */
36 31
    public function getActiveSheetName($filePath)
37
    {
38 31
        $xmlReader = $this->entityFactory->createXMLReader();
39 31
        if ($xmlReader->openFileInZip($filePath, self::SETTINGS_XML_FILE_PATH) === false) {
40 19
            return null;
41
        }
42
43 12
        $activeSheetName = null;
44
45
        try {
46 12
            while ($xmlReader->readUntilNodeFound(self::XML_NODE_CONFIG_ITEM)) {
47 12
                if ($xmlReader->getAttribute(self::XML_ATTRIBUTE_CONFIG_NAME) === self::XML_ATTRIBUTE_VALUE_ACTIVE_TABLE) {
48 12
                    $activeSheetName = $xmlReader->readString();
49 12
                    break;
50
                }
51
            }
52
        } catch (XMLProcessingException $exception) {
53
            // do nothing
54
        }
55
56 12
        $xmlReader->close();
57
58 12
        return $activeSheetName;
59
    }
60
}
61