Completed
Push — master ( 2f2459...d82a91 )
by Ross
02:33
created

DnsRecords::__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 DnsRecords
33
 * @package RossMitchell\UpdateCloudFlare\Data
34
 */
35
class DnsRecords implements RequestInterface
36
{
37
    /**
38
     * @var HeadersInterface
39
     */
40
    private $headers;
41
    /**
42
     * @var ConfigInterface
43
     */
44
    private $config;
45
    /**
46
     * @var SubDomainInfo
47
     */
48
    private $subDomainInfo;
49
50
    /**
51
     * DnsRecords constructor.
52
     *
53
     * @param ConfigInterface  $config
54
     * @param HeadersInterface $headers
55
     */
56 7
    public function __construct(ConfigInterface $config, HeadersInterface $headers)
57
    {
58 7
        $this->config  = $config;
59 7
        $this->headers = $headers;
60 7
    }
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 'GET';
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
     */
87 1
    public function getFields(): array
88
    {
89 1
        return [];
90
    }
91
92
    /**
93
     * Return the URL that the request should be made to
94
     *
95
     * @return string
96
     * @throws \RuntimeException
97
     * @throws LogicException
98
     */
99 1
    public function getUrl(): string
100
    {
101 1
        $baseUrl       = $this->config->getApiUrl();
102 1
        $subDomainInfo = $this->getSubDomainInfo();
103 1
        $zoneID        = $subDomainInfo->getZoneId();
104 1
        $type          = $subDomainInfo->getIpType();
105 1
        $subDomain     = $subDomainInfo->getSubDomain();
106 1
        $domain        = $this->config->getBaseUrl();
107
        $fullDomain    = "${subDomain}.${domain}";
108
109 1
        return "${baseUrl}zones/${zoneID}/dns_records?type=$type&name=$fullDomain";
110
    }
111
112
    /**
113
     * @return SubDomainInfo
114
     * @throws LogicException
115
     */
116
    public function getSubDomainInfo(): SubDomainInfo
117
    {
118 2
        if ($this->subDomainInfo === null) {
119
            throw new LogicException('You must set the sub domain info object');
120
        }
121
122 2
        return $this->subDomainInfo;
123
    }
124
125
    /**
126
     * @param SubDomainInfo $subDomainInfo
127
     *
128
     * @return DnsRecords
129
     */
130
    public function setSubDomainInfo(SubDomainInfo $subDomainInfo): DnsRecords
131
    {
132 7
        $this->subDomainInfo = $subDomainInfo;
133
134 7
        return $this;
135
    }
136
}
137