Test Failed
Branch develop (a9f1e9)
by Ludwig
02:28
created

MetadataEndpoint::uriHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    const ENDPOINT_LIST = 'servers/%s/zones/%s/metadata';
22
    const ENDPOINT_ELEMENT = 'servers/%s/zones/%s/metadata/%s';
23
24
    private $zoneId;
25
26
    public function __construct(Client $client, $defaultServerId, $zoneId)
27
    {
28
        $this->zoneId = $zoneId;
29
        parent::__construct($client, $defaultServerId);
30
    }
31
32
    /**
33
     * @param string $hydrationClass
34
     *
35
     * @return Metadata[]
36
     *
37
     * @throws \Http\Client\Exception
38
     */
39
    public function all($hydrationClass = Metadata::class): array
40
    {
41
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
42
43
        return $this->getClient()->call(null, $uri, $hydrationClass, true, 'GET');
44
    }
45
46
    /**
47
     * @param string $kind
48
     *
49
     * @return Metadata[]
50
     *
51
     * @throws \Http\Client\Exception
52
     */
53
    public function get(string $kind, $hydrationClass = Metadata::class): ?Metadata
54
    {
55
        return $this->getClient()->call(null, $this->uriHelper($kind), $hydrationClass, false, 'GET');
56
    }
57
58
    /**
59
     * @param Metadata $metadata
60
     * @param bool     $lacyLoad
61
     * @param string   $hydrationClass
62
     *
63
     * @return Metadata[]|null
64
     *
65
     * @throws \Http\Client\Exception
66
     */
67
    public function create(Metadata $metadata, $lacyLoad = true, $hydrationClass = Metadata::class): ?Metadata
68
    {
69
        $this->validateEntity($metadata, ['CREATE']);
70
71
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
72
        $payload = $this->getClient()->getSerializer()->serialize($metadata, 'json');
73
        $this->getClient()->call($payload, $uri, null, false, 'POST');
74
75
        if ($lacyLoad) {
76
            return $this->get($metadata->getKind(), $hydrationClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get($metad...ind(), $hydrationClass) also could return the type Cwd\PowerDNSClient\Model\Metadata which is incompatible with the documented return type Cwd\PowerDNSClient\Model\Metadata[]|null.
Loading history...
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * @param Metadata $metadata
84
     * @param bool     $lacyLoad
85
     *
86
     * @return Metadata[]|null
87
     *
88
     * @throws \Http\Client\Exception
89
     */
90
    public function update(Metadata $metadata, $lacyLoad = true, $hydrationClass = Metadata::class): ?Metadata
91
    {
92
        $this->validateEntity($metadata, ['UPDATE']);
93
        $payload = $this->getClient()->getSerializer()->serialize($metadata, 'json');
94
95
        $this->getClient()->call($payload, $this->uriHelper($metadata->getKind()), null, false, 'PUT');
96
97
        if ($lacyLoad) {
98
            return $this->get($metadata->getKind(), $hydrationClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get($metad...ind(), $hydrationClass) also could return the type Cwd\PowerDNSClient\Model\Metadata which is incompatible with the documented return type Cwd\PowerDNSClient\Model\Metadata[]|null.
Loading history...
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @param Metadata|string $metadata
106
     *
107
     * @throws \Http\Client\Exception
108
     */
109
    public function delete($metadata): void
110
    {
111
        $kind = $metadata;
112
        if ($metadata instanceof Metadata) {
113
            $kind = $metadata->getKind();
114
        }
115
        $this->getClient()->call(null, $this->uriHelper($kind), null, false, 'DELETE');
116
    }
117
118
    private function uriHelper($kind): string
119
    {
120
        return sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $this->zoneId, $kind);
121
    }
122
}
123