Passed
Push — master ( 0676a7...01ab94 )
by Artem
11:54
created

NodeBalancerNodeRepository::__construct()   A

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 3
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\NodeBalancers\Repository;
13
14
use Linode\Entity;
15
use Linode\Internal\AbstractRepository;
16
use Linode\LinodeClient;
17
use Linode\NodeBalancers\NodeBalancerNode;
18
use Linode\NodeBalancers\NodeBalancerNodeRepositoryInterface;
19
20
/**
21
 * @codeCoverageIgnore This class was autogenerated.
22
 */
23
class NodeBalancerNodeRepository extends AbstractRepository implements NodeBalancerNodeRepositoryInterface
24
{
25
    /**
26
     * @param int $nodeBalancerId The ID of the NodeBalancer to access.
27
     * @param int $configId       The ID of the NodeBalancer config to access.
28
     */
29
    public function __construct(LinodeClient $client, protected int $nodeBalancerId, protected int $configId)
30
    {
31
        parent::__construct($client);
32
    }
33
34
    public function createNodeBalancerNode(array $parameters = []): NodeBalancerNode
35
    {
36
        $response = $this->client->post($this->getBaseUri(), $parameters);
37
        $contents = $response->getBody()->getContents();
38
        $json     = json_decode($contents, true);
39
40
        return new NodeBalancerNode($this->client, $json);
41
    }
42
43
    public function updateNodeBalancerNode(int $nodeId, array $parameters = []): NodeBalancerNode
44
    {
45
        $response = $this->client->put(sprintf('%s/%s', $this->getBaseUri(), $nodeId), $parameters);
46
        $contents = $response->getBody()->getContents();
47
        $json     = json_decode($contents, true);
48
49
        return new NodeBalancerNode($this->client, $json);
50
    }
51
52
    public function deleteNodeBalancerConfigNode(int $nodeId): void
53
    {
54
        $this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $nodeId));
55
    }
56
57
    protected function getBaseUri(): string
58
    {
59
        return sprintf('/nodebalancers/%s/configs/%s/nodes', $this->nodeBalancerId, $this->configId);
60
    }
61
62
    protected function getSupportedFields(): array
63
    {
64
        return [
65
            NodeBalancerNode::FIELD_ID,
66
            NodeBalancerNode::FIELD_LABEL,
67
            NodeBalancerNode::FIELD_ADDRESS,
68
            NodeBalancerNode::FIELD_STATUS,
69
            NodeBalancerNode::FIELD_WEIGHT,
70
            NodeBalancerNode::FIELD_MODE,
71
            NodeBalancerNode::FIELD_NODEBALANCER_ID,
72
            NodeBalancerNode::FIELD_CONFIG_ID,
73
        ];
74
    }
75
76
    protected function jsonToEntity(array $json): Entity
77
    {
78
        return new NodeBalancerNode($this->client, $json);
79
    }
80
}
81