MetadataEndpoint   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 27
c 2
b 1
f 0
dl 0
loc 101
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A update() 0 12 2
A __construct() 0 4 1
A create() 0 13 2
A all() 0 5 1
A delete() 0 7 2
1
<?php
2
3
/*
4
 * This file is part of the CwdPowerDNS Client
5
 *
6
 * (c) 2018 cwd.at GmbH <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cwd\PowerDNSClient\Endpoints;
15
16
use Cwd\PowerDNSClient\Client;
17
use Cwd\PowerDNSClient\Model\Metadata;
18
19
class MetadataEndpoint extends AbstractEndpoint
20
{
21
    use UriHelperTrait;
22
23
    private const ENDPOINT_LIST = 'servers/%s/zones/%s/metadata';
24
    private const ENDPOINT_ELEMENT = 'servers/%s/zones/%s/metadata/%s';
25
26
    private $zoneId;
27
28
    public function __construct(Client $client, $defaultServerId, $zoneId)
29
    {
30
        $this->zoneId = $zoneId;
31
        parent::__construct($client, $defaultServerId);
32
    }
33
34
    /**
35
     * @param string $hydrationClass
36
     *
37
     * @return Metadata[]
38
     *
39
     * @throws \Http\Client\Exception
40
     */
41
    public function all($hydrationClass = Metadata::class): array
42
    {
43
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
44
45
        return $this->getClient()->call(null, $uri, $hydrationClass, true, 'GET');
46
    }
47
48
    /**
49
     * @param string $kind
50
     * @param string $hydrationClass
51
     *
52
     * @return Metadata
53
     *
54
     * @throws \Http\Client\Exception
55
     */
56
    public function get(string $kind, string $hydrationClass = Metadata::class): ?Metadata
57
    {
58
        return $this->getClient()->call(null, $this->uriHelper($kind), $hydrationClass, false, 'GET');
59
    }
60
61
    /**
62
     * @param Metadata $metadata
63
     * @param bool     $lacyLoad
64
     * @param string   $hydrationClass
65
     *
66
     * @return Metadata|null
67
     *
68
     * @throws \Http\Client\Exception
69
     */
70
    public function create(Metadata $metadata, $lacyLoad = true, $hydrationClass = Metadata::class): ?Metadata
71
    {
72
        $this->validateEntity($metadata, ['CREATE']);
73
74
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
75
        $payload = $this->getClient()->getSerializer()->serialize($metadata, 'json');
76
        $this->getClient()->call($payload, $uri, null, false, 'POST');
77
78
        if ($lacyLoad) {
79
            return $this->get($metadata->getKind(), $hydrationClass);
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * @param Metadata $metadata
87
     * @param bool     $lacyLoad
88
     * @param string   $hydrationClass
89
     *
90
     * @return Metadata|null
91
     *
92
     * @throws \Http\Client\Exception
93
     */
94
    public function update(Metadata $metadata, $lacyLoad = true, string $hydrationClass = Metadata::class): ?Metadata
95
    {
96
        $this->validateEntity($metadata, ['UPDATE']);
97
        $payload = $this->getClient()->getSerializer()->serialize($metadata, 'json');
98
99
        $this->getClient()->call($payload, $this->uriHelper($metadata->getKind()), null, false, 'PUT');
100
101
        if ($lacyLoad) {
102
            return $this->get($metadata->getKind(), $hydrationClass);
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @param Metadata|string $metadata
110
     *
111
     * @throws \Http\Client\Exception
112
     */
113
    public function delete($metadata): void
114
    {
115
        $kind = $metadata;
116
        if ($metadata instanceof Metadata) {
117
            $kind = $metadata->getKind();
118
        }
119
        $this->getClient()->call(null, $this->uriHelper($kind), null, false, 'DELETE');
120
    }
121
}
122