Config::getApiKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Data;
24
25
use RossMitchell\UpdateCloudFlare\Exceptions\MissingConfigException;
26
use RossMitchell\UpdateCloudFlare\Interfaces\ConfigInterface;
27
use Symfony\Component\Console\Exception\LogicException;
28
29
/**
30
 * Class Config
31
 * @package RossMitchell\UpdateCloudFlare\Data
32
 */
33
class Config implements ConfigInterface
34
{
35
    public const API_KEY       = 'api.credentials.key';
36
    public const API_URL       = 'api.url';
37
    public const BASE_DOMAIN   = 'domain_details.base_url';
38
    public const EMAIL_ADDRESS = 'api.credentials.email';
39
    public const SUB_DOMAINS   = 'domain_details.sub_domains';
40
    private $configDetails;
41
42
    /**
43
     * Config constructor.
44
     *
45
     * @param \PHLAK\Config\Config $config
46
     * @param string               $configFile
47
     *
48
     * @throws MissingConfigException
49
     */
50 38
    public function __construct(\PHLAK\Config\Config $config, string $configFile)
51
    {
52
        try {
53 38
            $config->load($configFile);
54 1
        } catch (\Error $error) {
55 1
            $code      = $error->getCode();
56 1
            $exception = new MissingConfigException("Could not find config file $configFile", $code, $error);
57 1
            throw $exception;
58
59
        }
60 37
        $this->configDetails = $config;
61 37
    }
62
63
    /**
64
     * @return string
65
     */
66 5
    public function getEmailAddress(): string
67
    {
68 5
        return (string) $this->get(self::EMAIL_ADDRESS);
69
    }
70
71
    /**
72
     * @param string $path
73
     *
74
     * @return mixed
75
     */
76 15
    private function get(string $path)
77
    {
78 15
        return $this->configDetails->get($path);
79
    }
80
81
    /**
82
     * @return string
83
     */
84 5
    public function getApiKey(): string
85
    {
86 5
        return (string) $this->get(self::API_KEY);
87
    }
88
89
    /**
90
     * @return string
91
     */
92 4
    public function getBaseUrl(): string
93
    {
94 4
        return (string) $this->get(self::BASE_DOMAIN);
95
    }
96
97
    /**
98
     * @return string
99
     */
100 4
    public function getApiUrl(): string
101
    {
102 4
        return (string) $this->get(self::API_URL);
103
    }
104
105
    /**
106
     * @return array
107
     * @throws LogicException
108
     */
109 3
    public function getSubDomains(): array
110
    {
111 3
        $subDomains = $this->get(self::SUB_DOMAINS);
112 3
        if (!\is_array($subDomains) && \is_string($subDomains)) {
113 1
            $subDomains = [$subDomains];
114
        }
115
116 3
        if (!\is_array($subDomains)) {
117 1
            throw new LogicException('The Sub Domains node must be a string or an array');
118
        }
119
120 2
        return $subDomains;
121
    }
122
}
123