Passed
Push — master ( c53e9c...76439b )
by Ross
02:52
created

GetSubDomainInfo::getSubDomainIpAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
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\Model\Requests;
24
25
use RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException;
26
use RossMitchell\UpdateCloudFlare\Interfaces\ConfigInterface;
27
use RossMitchell\UpdateCloudFlare\Interfaces\CurlInterface;
28
use RossMitchell\UpdateCloudFlare\Interfaces\HeadersInterface;
29
use RossMitchell\UpdateCloudFlare\Interfaces\RequestInterface;
30
use Symfony\Component\Console\Exception\LogicException;
31
32
/**
33
 * Class GetSubDomainInfo
34
 * @package RossMitchell\UpdateCloudFlare\Data
35
 */
36
class GetSubDomainInfo implements RequestInterface
37
{
38
    /**
39
     * @var HeadersInterface
40
     */
41
    private $headers;
42
    /**
43
     * @var ConfigInterface
44
     */
45
    private $config;
46
    /**
47
     * @var GetDnsZones
48
     */
49
    private $dnsZones;
50
    /**
51
     * @var string
52
     */
53
    private $subDomainId;
54
    /**
55
     * @var string
56
     */
57
    private $subDomainIpAddress;
58
    /**
59
     * @var string
60
     */
61
    private $subDomain;
62
    /**
63
     * @var CurlInterface
64
     */
65
    private $curl;
66
67
    /**
68
     * GetSubDomainInfo constructor.
69
     *
70
     * @param HeadersInterface $headers
71
     * @param ConfigInterface  $config
72
     * @param GetDnsZones      $dnsZones
73
     * @param CurlInterface    $curl
74
     */
75
    public function __construct(HeadersInterface $headers, ConfigInterface $config, GetDnsZones $dnsZones, CurlInterface $curl)
76
    {
77
        $this->headers  = $headers;
78
        $this->config   = $config;
79
        $this->dnsZones = $dnsZones;
80
        $this->curl     = $curl;
81
    }
82
83
    /**
84
     * @return string
85
     * @throws LogicException
86
     * @throws \RuntimeException
87
     * @throws CloudFlareException
88
     */
89
    public function getSubDomainId(): string
90
    {
91
        if ($this->subDomainId === null) {
92
            $this->collectionInformation();
93
        }
94
95
        return $this->subDomainId;
96
    }
97
98
    /**
99
     * @return string
100
     * @throws LogicException
101
     * @throws \RuntimeException
102
     * @throws CloudFlareException
103
     */
104
    public function getSubDomainIpAddress(): string
105
    {
106
        if ($this->subDomainIpAddress === null) {
107
            $this->collectionInformation();
108
        }
109
110
        return $this->subDomainIpAddress;
111
    }
112
113
    /**
114
     * @return string
115
     * @throws LogicException
116
     */
117
    public function getSubDomain(): string
118
    {
119
        if ($this->subDomain === null) {
120
            throw new LogicException('You must set the sub domain');
121
        }
122
123
        return $this->subDomain;
124
    }
125
126
    /**
127
     * @param string $subDomain
128
     *
129
     * @return GetSubDomainInfo
130
     */
131
    public function setSubDomain(string $subDomain): GetSubDomainInfo
132
    {
133
        $this->subDomain = $subDomain;
134
135
        return $this;
136
    }
137
138
    /**
139
     * If headers need to be sent through then they can be returned with this method. If not return an empty array
140
     *
141
     * @return array
142
     */
143
    public function getHeaders(): array
144
    {
145
        return $this->headers->getHeadersArray();
146
    }
147
148
    /**
149
     * They type of request that is going to be made
150
     *
151
     * @return string
152
     */
153
    public function getRequestType(): string
154
    {
155
        return 'GET';
156
    }
157
158
    /**
159
     * If the request needs data to be sent though return it here. If not return an empty array
160
     *
161
     * @return array
162
     */
163
    public function getFields(): array
164
    {
165
        return [];
166
    }
167
168
    /**
169
     * Return the URL that the request should be made to
170
     *
171
     * @return string
172
     * @throws \RuntimeException
173
     * @throws LogicException
174
     * @throws CloudFlareException
175
     */
176
    public function getUrl(): string
177
    {
178
        $baseUrl     = $this->config->getApiUrl();
179
        $zoneID      = $this->dnsZones->getZoneInformation();
180
        $type        = 'A';
181
        $subDomain   = $this->getSubDomain();
182
        $domain      = $this->config->getBaseUrl();
183
        $fullDomain = "${subDomain}.${domain}";
184
185
186
        return "${baseUrl}zones/${zoneID}/dns_records?type=$type&name=$fullDomain";
187
    }
188
189
    /**
190
     * @throws LogicException
191
     * @throws \RuntimeException
192
     * @throws CloudFlareException
193
     */
194
    private function collectionInformation()
195
    {
196
        $result = \json_decode($this->curl->makeRequest($this));
197
        if ($result->success === false) {
198
            $error = new CloudFlareException();
199
            $error->setDetails($result, self::class);
0 ignored issues
show
Unused Code introduced by
The call to RossMitchell\UpdateCloud...Exception::setDetails() has too many arguments starting with self::class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
            $error->/** @scrutinizer ignore-call */ 
200
                    setDetails($result, self::class);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
200
            throw $error;
201
        }
202
        $details                  = $result->result[0];
203
        $this->subDomainId        = $details->id;
204
        $this->subDomainIpAddress = $details->content;
205
    }
206
}
207