Completed
Pull Request — develop_3.0 (#458)
by Adrien
01:54
created

SettingsHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 49
ccs 14
cts 15
cp 0.9333
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getActiveSheetName() 0 24 5
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\EntityFactory;
7
use Box\Spout\Reader\Wrapper\XMLReader;
8
9
/**
10
 * Class SettingsHelper
11
 * This class provides helper functions to extract data from the "settings.xml" file.
12
 *
13
 * @package Box\Spout\Reader\ODS\Helper
14
 */
15
class SettingsHelper
16
{
17
    const SETTINGS_XML_FILE_PATH = 'settings.xml';
18
19
    /** Definition of XML nodes name and attribute used to parse settings data */
20
    const XML_NODE_CONFIG_ITEM = 'config:config-item';
21
    const XML_ATTRIBUTE_CONFIG_NAME = 'config:name';
22
    const XML_ATTRIBUTE_VALUE_ACTIVE_TABLE = 'ActiveTable';
23
24
    /** @var EntityFactory Factory to create entities */
25
    private $entityFactory;
26
27
    /**
28
     * @param EntityFactory $entityFactory Factory to create entities
29
     */
30 30
    public function __construct($entityFactory)
31
    {
32 30
        $this->entityFactory = $entityFactory;
33 30
    }
34
35
    /**
36
     * @param string $filePath Path of the file to be read
37
     * @return string|null Name of the sheet that was defined as active or NULL if none found
38
     */
39 30
    public function getActiveSheetName($filePath)
40
    {
41 30
        $xmlReader = $this->entityFactory->createXMLReader();
42 30
        if ($xmlReader->openFileInZip($filePath, self::SETTINGS_XML_FILE_PATH) === false) {
43 19
            return null;
44
        }
45
46 11
        $activeSheetName = null;
47
48
        try {
49 11
            while ($xmlReader->readUntilNodeFound(self::XML_NODE_CONFIG_ITEM)) {
50 11
                if ($xmlReader->getAttribute(self::XML_ATTRIBUTE_CONFIG_NAME) === self::XML_ATTRIBUTE_VALUE_ACTIVE_TABLE) {
51 11
                    $activeSheetName = $xmlReader->readString();
52 11
                    break;
53
                }
54
            }
55
        } catch (XMLProcessingException $exception) {
56
            // do nothing
57
        }
58
59 11
        $xmlReader->close();
60
61 11
        return $activeSheetName;
62
    }
63
}
64