Test Failed
Branch develop (62fe73)
by Ludwig
02:25
created

CryptokeysEndpoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 5 1
A __construct() 0 4 1
A deactivate() 0 3 1
A get() 0 9 2
A setStatus() 0 16 3
A activate() 0 3 1
A create() 0 8 1
A delete() 0 8 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\Cryptokey;
18
19
class CryptokeysEndpoint extends AbstractEndpoint
20
{
21
    use UriHelperTrait;
22
23
    private const ENDPOINT_LIST = 'servers/%s/zones/%s/cryptokeys';
24
    private const ENDPOINT_ELEMENT = 'servers/%s/zones/%s/cryptokeys/%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
    public function all($hydrationClass = Cryptokey::class): array
35
    {
36
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
37
38
        return $this->getClient()->call(null, $uri, $hydrationClass, true, 'GET');
39
    }
40
41
    public function get($cryptokey, $hydrationClass = Cryptokey::class): ?Cryptokey
42
    {
43
        $cryptokeyId = $cryptokey;
44
45
        if ($cryptokey instanceof Cryptokey) {
46
            $cryptokeyId = $cryptokey->getId();
47
        }
48
49
        return $this->getClient()->call(null, $this->uriHelper($cryptokeyId), $hydrationClass, false, 'GET');
50
    }
51
52
    public function create(Cryptokey $cryptokey, $hydrationClass = Cryptokey::class): ?Cryptokey
53
    {
54
        $this->validateEntity($cryptokey, ['CREATE']);
55
56
        $uri = sprintf(self::ENDPOINT_LIST, $this->defaultServerId, $this->zoneId);
57
        $payload = $this->getClient()->getSerializer()->serialize($cryptokey, 'json');
58
59
        return $this->getClient()->call($payload, $uri, $hydrationClass, false, 'POST');
60
    }
61
62
    public function activate($cryptokey, $lacyLoad = true, $hydrationClass = Cryptokey::class): ?Cryptokey
63
    {
64
        return $this->setStatus(true, $cryptokey, $lacyLoad, $hydrationClass);
65
    }
66
67
    public function deactivate($cryptokey, $lacyLoad = true, $hydrationClass = Cryptokey::class): ?Cryptokey
68
    {
69
        return $this->setStatus(false, $cryptokey, $lacyLoad, $hydrationClass);
70
    }
71
72
    private function setStatus(bool $active, $cryptokey, $lacyLoad = true, $hydrationClass = Cryptokey::class): ?Cryptokey
73
    {
74
        $cryptokeyId = $cryptokey;
75
        if ($cryptokey instanceof Cryptokey) {
76
            $cryptokeyId = $cryptokey->getId();
77
        }
78
79
        $payload = $this->getClient()->getSerializer()->serialize(['active' => $active], 'json');
80
81
        $this->getClient()->call($payload, $this->uriHelper($cryptokeyId), null, false, 'PUT');
82
83
        if ($lacyLoad) {
84
            return $this->get($cryptokeyId, $hydrationClass);
85
        }
86
87
        return null;
88
    }
89
90
    public function delete($cryptokey): void
91
    {
92
        $cryptokeyId = $cryptokey;
93
        if ($cryptokey instanceof Cryptokey) {
94
            $cryptokeyId = $cryptokey->getId();
95
        }
96
97
        $this->getClient()->call(null, $this->uriHelper($cryptokeyId), null, false, 'DELETE');
98
    }
99
}
100