Test Setup Failed
Push — master ( 7a093e...068245 )
by Nick
05:17
created

MagentoOne   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 108
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getXmlDoc() 0 8 2
A xpath() 0 10 2
A getDatabaseHost() 0 4 1
A getDatabasePort() 0 4 1
A getDatabaseUsername() 0 4 1
A getDatabasePassword() 0 4 1
A getDatabaseName() 0 4 1
A getInstallDate() 0 4 1
A getAdminFrontName() 0 4 1
A getDsnParser() 0 5 1
1
<?php
2
3
namespace Meanbee\LibMageConf\ConfigReader;
4
5
use Meanbee\LibMageConf\ConfigReader;
6
use Meanbee\LibMageConf\Exception\FileNotFound;
7
use Meanbee\LibMageConf\Util\DsnParser;
8
9
class MagentoOne implements ConfigReader
10
{
11
    protected $configFile;
12
    protected $xmlDoc;
13
14
    public function __construct($configFile)
15
    {
16
        $this->configFile = $configFile;
17
18
        if (!file_exists($this->configFile)) {
19
            throw new FileNotFound(
20
                sprintf("Unable to find config file at %s", $this->configFile)
21
            );
22
        }
23
    }
24
25
    /**
26
     * @return \SimpleXMLElement
27
     */
28
    protected function getXmlDoc()
29
    {
30
        if ($this->xmlDoc === null) {
31
            $this->xmlDoc = new \SimpleXMLElement(file_get_contents($this->configFile));
32
        }
33
34
        return $this->xmlDoc;
35
    }
36
37
    /**
38
     * @param $xpath
39
     * @return null|string
40
     */
41
    public function xpath($xpath)
42
    {
43
        $result = $this->getXmlDoc()->xpath($xpath);
44
45
        if (count($result) > 0) {
46
            return (string) $result[0];
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * @return null|string
54
     */
55
    public function getDatabaseHost()
56
    {
57
        return $this->getDsnParser()->getHost();
58
    }
59
60
    /**
61
     * @return null|string
62
     */
63
    public function getDatabasePort()
64
    {
65
        return $this->getDsnParser()->getPort();
66
    }
67
68
    /**
69
     * @return null|string
70
     */
71
    public function getDatabaseUsername()
72
    {
73
        return $this->xpath('//config/global/resources/default_setup/connection/username');
74
    }
75
76
    /**
77
     * @return null|string
78
     */
79
    public function getDatabasePassword()
80
    {
81
        return $this->xpath('//config/global/resources/default_setup/connection/password');
82
    }
83
84
    /**
85
     * @return null|string
86
     */
87
    public function getDatabaseName()
88
    {
89
        return $this->xpath('//config/global/resources/default_setup/connection/dbname');
90
    }
91
92
    /**
93
     * @return null|string
94
     */
95
    public function getInstallDate()
96
    {
97
        return $this->xpath('//config/global/install/date');
98
    }
99
100
    /**
101
     * @return null|string
102
     */
103
    public function getAdminFrontName()
104
    {
105
        return $this->xpath('//config/admin/routers/adminhtml/args/frontName');
106
    }
107
108
    /**
109
     * @return DsnParser
110
     */
111
    protected function getDsnParser()
112
    {
113
        $rawHost = $this->xpath('//config/global/resources/default_setup/connection/host');
114
        return new DsnParser($rawHost);
115
    }
116
}