IPAddressRepository::getBaseUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
// ---------------------------------------------------------------------
4
//
5
//  Copyright (C) 2018-2024 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <https://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode\LinodeInstances\Repository;
13
14
use Linode\Entity;
15
use Linode\Internal\AbstractRepository;
16
use Linode\LinodeClient;
17
use Linode\LinodeInstances\IPAddressRepositoryInterface;
18
use Linode\LinodeInstances\NetworkInformation;
19
use Linode\Networking\IPAddress;
20
21
/**
22
 * @codeCoverageIgnore This class was autogenerated.
23
 */
24
class IPAddressRepository extends AbstractRepository implements IPAddressRepositoryInterface
25
{
26
    /**
27
     * @param int $linodeId ID of the Linode to look up.
28
     */
29
    public function __construct(LinodeClient $client, protected int $linodeId)
30
    {
31
        parent::__construct($client);
32
    }
33
34
    public function getLinodeIPs(): NetworkInformation
35
    {
36
        $response = $this->client->get($this->getBaseUri());
37
        $contents = $response->getBody()->getContents();
38
        $json     = json_decode($contents, true);
39
40
        return new NetworkInformation($this->client, $json);
41
    }
42
43
    public function getLinodeIP(string $address): IPAddress
44
    {
45
        $response = $this->client->get(sprintf('%s/%s', $this->getBaseUri(), $address));
46
        $contents = $response->getBody()->getContents();
47
        $json     = json_decode($contents, true);
48
49
        return new IPAddress($this->client, $json);
50
    }
51
52
    public function addLinodeIP(array $parameters = []): IPAddress
53
    {
54
        $response = $this->client->post($this->getBaseUri(), $parameters);
55
        $contents = $response->getBody()->getContents();
56
        $json     = json_decode($contents, true);
57
58
        return new IPAddress($this->client, $json);
59
    }
60
61
    public function updateLinodeIP(string $address, array $parameters = []): IPAddress
62
    {
63
        $response = $this->client->put(sprintf('%s/%s', $this->getBaseUri(), $address), $parameters);
64
        $contents = $response->getBody()->getContents();
65
        $json     = json_decode($contents, true);
66
67
        return new IPAddress($this->client, $json);
68
    }
69
70
    public function removeLinodeIP(string $address): void
71
    {
72
        $this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $address));
73
    }
74
75
    protected function getBaseUri(): string
76
    {
77
        return sprintf('/linode/instances/%s/ips', $this->linodeId);
78
    }
79
80
    protected function getSupportedFields(): array
81
    {
82
        return [
83
            IPAddress::FIELD_ADDRESS,
84
            IPAddress::FIELD_TYPE,
85
            IPAddress::FIELD_PUBLIC,
86
            IPAddress::FIELD_RDNS,
87
            IPAddress::FIELD_REGION,
88
            IPAddress::FIELD_LINODE_ID,
89
            IPAddress::FIELD_GATEWAY,
90
            IPAddress::FIELD_SUBNET_MASK,
91
            IPAddress::FIELD_PREFIX,
92
        ];
93
    }
94
95
    protected function jsonToEntity(array $json): Entity
96
    {
97
        return new IPAddress($this->client, $json);
98
    }
99
}
100