Passed
Push — master ( ac5429...699403 )
by Ross
02:06
created

UpdateDnsRecords::getSubDomainInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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\Data
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
     */
56
    public function __construct(ConfigInterface $config, HeadersInterface $headers)
57
    {
58
        $this->config  = $config;
59
        $this->headers = $headers;
60
    }
61
62
    /**
63
     * If headers need to be sent through then they can be returned with this method. If not return an empty array
64
     *
65
     * @return array
66
     */
67
    public function getHeaders(): array
68
    {
69
        return $this->headers->getHeadersArray();
70
    }
71
72
    /**
73
     * They type of request that is going to be made
74
     *
75
     * @return string
76
     */
77
    public function getRequestType(): string
78
    {
79
        return 'PUT';
80
    }
81
82
    /**
83
     * If the request needs data to be sent though return it here. If not return an empty array
84
     *
85
     * @return array
86
     * @throws LogicException
87
     */
88
    public function getFields(): array
89
    {
90
        $subDomainInfo = $this->getSubDomainInfo();
91
        $subDomain     = $subDomainInfo->getSubDomain();
92
        $ip            = $subDomainInfo->getIpAddress();
93
        $type          = $subDomainInfo->getIpType();
94
        $domain        = $this->config->getBaseUrl();
95
        $fullDomain    = "${subDomain}.${domain}";
96
97
        return [
98
            'type'    => $type,
99
            'name'    => $fullDomain,
100
            'content' => $ip,
101
        ];
102
    }
103
104
    /**
105
     * Return the URL that the request should be made to
106
     *
107
     * @return string
108
     * @throws LogicException
109
     * @throws \RuntimeException
110
     */
111
    public function getUrl(): string
112
    {
113
        $baseUrl       = $this->config->getApiUrl();
114
        $subDomainInfo = $this->getSubDomainInfo();
115
        $zoneID        = $subDomainInfo->getZoneId();
116
        $subDomainId   = $subDomainInfo->getSubDomainId();
117
118
        return "${baseUrl}zones/${zoneID}/dns_records/${subDomainId}";
119
    }
120
121
    /**
122
     * @return SubDomainInfo
123
     * @throws LogicException
124
     */
125
    public function getSubDomainInfo(): SubDomainInfo
126
    {
127
        if ($this->subDomainInfo === null) {
128
            throw new LogicException('You must set the sub domain info object');
129
        }
130
131
        return $this->subDomainInfo;
132
    }
133
134
    /**
135
     * @param SubDomainInfo $subDomainInfo
136
     *
137
     * @return UpdateDnsRecords
138
     */
139
    public function setSubDomainInfo(SubDomainInfo $subDomainInfo): UpdateDnsRecords
140
    {
141
        $this->subDomainInfo = $subDomainInfo;
142
143
        return $this;
144
    }
145
146
}
147