Completed
Push — master ( 4d3fdb...fd1b5e )
by Ross
02:40
created

UpdateDnsRecords::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\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 5
    public function __construct(ConfigInterface $config, HeadersInterface $headers)
57
    {
58 5
        $this->config  = $config;
59 5
        $this->headers = $headers;
60 5
    }
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 1
    public function getHeaders(): array
68
    {
69 1
        return $this->headers->getHeadersArray();
70
    }
71
72
    /**
73
     * They type of request that is going to be made
74
     *
75
     * @return string
76
     */
77 1
    public function getRequestType(): string
78
    {
79 1
        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 1
    public function getFields(): array
89
    {
90 1
        $subDomainInfo = $this->getSubDomainInfo();
91 1
        $subDomain     = $subDomainInfo->getSubDomain();
92 1
        $ip            = $subDomainInfo->getIpAddress();
93 1
        $type          = $subDomainInfo->getIpType();
94 1
        $domain        = $this->config->getBaseUrl();
95
        $fullDomain    = "${subDomain}.${domain}";
96
97
        return [
98 1
            'type'    => $type,
99 1
            'name'    => $fullDomain,
100 1
            '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 1
        $baseUrl       = $this->config->getApiUrl();
114 1
        $subDomainInfo = $this->getSubDomainInfo();
115 1
        $zoneID        = $subDomainInfo->getZoneId();
116 1
        $subDomainId   = $subDomainInfo->getSubDomainId();
117
118 1
        return "${baseUrl}zones/${zoneID}/dns_records/${subDomainId}";
119
    }
120
121
    /**
122
     * @return SubDomainInfo
123
     * @throws LogicException
124
     */
125
    public function getSubDomainInfo(): SubDomainInfo
126
    {
127 2
        if ($this->subDomainInfo === null) {
128
            throw new LogicException('You must set the sub domain info object');
129
        }
130
131 2
        return $this->subDomainInfo;
132
    }
133
134
    /**
135
     * @param SubDomainInfo $subDomainInfo
136
     *
137
     * @return UpdateDnsRecords
138
     */
139
    public function setSubDomainInfo(SubDomainInfo $subDomainInfo): UpdateDnsRecords
140
    {
141 5
        $this->subDomainInfo = $subDomainInfo;
142
143 5
        return $this;
144
    }
145
146
}
147