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

UpdateDnsRecord::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 5
crap 2
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 UpdateDnsRecord
34
 * @package RossMitchell\UpdateCloudFlare\Data
35
 */
36
class UpdateDnsRecord implements RequestInterface
37
{
38
    /**
39
     * @var ConfigInterface
40
     */
41
    private $config;
42
    /**
43
     * @var HeadersInterface
44
     */
45
    private $headers;
46
    /**
47
     * @var GetSubDomainInfo
48
     */
49
    private $subDomainInfo;
50
    /**
51
     * @var GetDnsZones
52
     */
53
    private $dnsZones;
54
    /**
55
     * @var string
56
     */
57
    private $ipAddress;
58
    /**
59
     * @var string
60
     */
61
    private $subDomain;
62
    /**
63
     * @var CurlInterface
64
     */
65
    private $curl;
66
67
    /**
68
     * UpdateDnsRecord constructor.
69
     *
70
     * @param ConfigInterface  $config
71
     * @param HeadersInterface $headers
72
     * @param GetDnsZones      $dnsZones
73
     * @param GetSubDomainInfo $subDomainInfo
74
     * @param CurlInterface    $curl
75
     */
76
    public function __construct(
77
        ConfigInterface $config,
78
        HeadersInterface $headers,
79
        GetDnsZones $dnsZones,
80
        GetSubDomainInfo $subDomainInfo,
81
        CurlInterface $curl
82
    ) {
83
        $this->config        = $config;
84
        $this->headers       = $headers;
85
        $this->subDomainInfo = $subDomainInfo;
86
        $this->dnsZones      = $dnsZones;
87
        $this->curl          = $curl;
88
    }
89
90
    /**
91
     * @param string $ipAddress
92
     *
93
     * @return UpdateDnsRecord
94
     */
95
    public function setNewIpAddress(string $ipAddress): UpdateDnsRecord
96
    {
97
        $this->ipAddress = $ipAddress;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     * @throws LogicException
105
     */
106
    public function getIpAddress(): string
107
    {
108
        if ($this->ipAddress === null) {
109
            throw new LogicException('You must set the new IP Address');
110
        }
111
112
        return $this->ipAddress;
113
    }
114
115
    /**
116
     * @return string
117
     * @throws LogicException
118
     */
119
    public function getSubDomain(): string
120
    {
121
        if ($this->subDomain === null) {
122
            throw new LogicException('You must set the sub domain');
123
        }
124
125
        return $this->subDomain;
126
    }
127
128
    /**
129
     * @param string $subDomain
130
     *
131
     * @return UpdateDnsRecord
132
     */
133
    public function setSubDomain(string $subDomain): UpdateDnsRecord
134
    {
135
        $this->subDomain = $subDomain;
136
        $this->subDomainInfo->setSubDomain($subDomain);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return string
143
     * @throws LogicException
144
     * @throws \RuntimeException
145
     * @throws CloudFlareException
146
     */
147
    public function changeIpAddress(): string
148
    {
149
        $result = \json_decode($this->curl->makeRequest($this));
150
        if ($result->success === false) {
151
            $error = new CloudFlareException();
152
            $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

152
            $error->/** @scrutinizer ignore-call */ 
153
                    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...
153
            throw $error;
154
        }
155
156
        $details = $result->result;
157
        $host    = $details->name;
158
        $newIp   = $details->content;
159
160
        return "${host} has had its IP address update to ${newIp}";
161
    }
162
163
    /**
164
     * If headers need to be sent through then they can be returned with this method. If not return an empty array
165
     *
166
     * @return array
167
     */
168
    public function getHeaders(): array
169
    {
170
        return $this->headers->getHeadersArray();
171
    }
172
173
    /**
174
     * They type of request that is going to be made
175
     *
176
     * @return string
177
     */
178
    public function getRequestType(): string
179
    {
180
        return 'PUT';
181
    }
182
183
    /**
184
     * If the request needs data to be sent though return it here. If not return an empty array
185
     *
186
     * @return array
187
     * @throws LogicException
188
     */
189
    public function getFields(): array
190
    {
191
        $subDomain   = $this->getSubDomain();
192
        $domain      = $this->config->getBaseUrl();
193
        $fullDomain = "${subDomain}.${domain}";
194
        $type        = 'A';
195
        $ip          = $this->getIpAddress();
196
197
        return [
198
            'type'    => $type,
199
            'name'    => $fullDomain,
200
            'content' => $ip,
201
        ];
202
    }
203
204
    /**
205
     * Return the URL that the request should be made to
206
     *
207
     * @return string
208
     * @throws LogicException
209
     * @throws \RuntimeException
210
     * @throws CloudFlareException
211
     */
212
    public function getUrl(): string
213
    {
214
        $baseUrl     = $this->config->getApiUrl();
215
        $zoneID      = $this->dnsZones->getZoneInformation();
216
        $subDomainId = $this->subDomainInfo->getSubDomainId();
217
218
        return "${baseUrl}zones/${zoneID}/dns_records/${subDomainId}";
219
    }
220
}
221