UpdateDnsRecords   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 94
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 13 1
A getHeaders() 0 3 1
A getSubDomainInfo() 0 3 1
A __construct() 0 5 1
A getRequestType() 0 3 1
A getUrl() 0 8 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\Requests;
24
25
use RossMitchell\UpdateCloudFlare\Data\SubDomainInfo;
26
use RossMitchell\UpdateCloudFlare\Interfaces\ConfigInterface;
27
use RossMitchell\UpdateCloudFlare\Interfaces\HeadersInterface;
28
use RossMitchell\UpdateCloudFlare\Interfaces\RequestInterface;
29
use Symfony\Component\Console\Exception\LogicException;
30
31
/**
32
 * Class UpdateDnsRecord
33
 * @package RossMitchell\UpdateCloudFlare\Request
34
 */
35
class UpdateDnsRecords implements RequestInterface
36
{
37
    /**
38
     * @var ConfigInterface
39
     */
40
    private $config;
41
    /**
42
     * @var HeadersInterface
43
     */
44
    private $headers;
45
    /**
46
     * @var SubDomainInfo
47
     */
48
    private $subDomainInfo;
49
50
    /**
51
     * UpdateDnsRecord constructor.
52
     *
53
     * @param ConfigInterface  $config
54
     * @param HeadersInterface $headers
55
     * @param SubDomainInfo    $subDomainInfo
56
     */
57 9
    public function __construct(ConfigInterface $config, HeadersInterface $headers, SubDomainInfo $subDomainInfo)
58
    {
59 9
        $this->config        = $config;
60 9
        $this->headers       = $headers;
61 9
        $this->subDomainInfo = $subDomainInfo;
62 9
    }
63
64
    /**
65
     * If headers need to be sent through then they can be returned with this method. If not return an empty array
66
     *
67
     * @return array
68
     */
69 1
    public function getHeaders(): array
70
    {
71 1
        return $this->headers->getHeadersArray();
72
    }
73
74
    /**
75
     * They type of request that is going to be made
76
     *
77
     * @return string
78
     */
79 1
    public function getRequestType(): string
80
    {
81 1
        return 'PUT';
82
    }
83
84
    /**
85
     * If the request needs data to be sent though return it here. If not return an empty array
86
     *
87
     * @return array
88
     * @throws LogicException
89
     */
90 1
    public function getFields(): array
91
    {
92 1
        $subDomainInfo = $this->getSubDomainInfo();
93 1
        $subDomain     = $subDomainInfo->getSubDomain();
94 1
        $ip            = $subDomainInfo->getIpAddress();
95 1
        $type          = $subDomainInfo->getIpType();
96 1
        $domain        = $this->config->getBaseUrl();
97
        $fullDomain    = "${subDomain}.${domain}";
98
99
        return [
100 1
            'type'    => $type,
101 1
            'name'    => $fullDomain,
102 1
            'content' => $ip,
103
        ];
104
    }
105
106
    /**
107
     * Return the URL that the request should be made to
108
     *
109
     * @return string
110
     * @throws LogicException
111
     * @throws \RuntimeException
112
     */
113
    public function getUrl(): string
114
    {
115 1
        $baseUrl       = $this->config->getApiUrl();
116 1
        $subDomainInfo = $this->getSubDomainInfo();
117 1
        $zoneID        = $subDomainInfo->getZoneId();
118 1
        $subDomainId   = $subDomainInfo->getSubDomainId();
119
120 1
        return "${baseUrl}zones/${zoneID}/dns_records/${subDomainId}";
121
    }
122
123
    /**
124
     * @return SubDomainInfo
125
     */
126
    public function getSubDomainInfo(): SubDomainInfo
127
    {
128 5
        return $this->subDomainInfo;
129
    }
130
}
131