SecretKey::_get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
// Copyright 1999-2021. Plesk International GmbH.
3
4
namespace PleskX\Api\Operator;
5
6
use PleskX\Api\Struct\SecretKey as Struct;
7
8
class SecretKey extends \PleskX\Api\Operator
9
{
10
    protected $_wrapperTag = 'secret_key';
11
12
    /**
13
     * @param string $ipAddress
14
     * @param string $description
15
     *
16
     * @return string
17
     */
18
    public function create($ipAddress = '', $description = '')
19
    {
20
        $packet = $this->_client->getPacket();
21
        $createTag = $packet->addChild($this->_wrapperTag)->addChild('create');
22
23
        if ('' !== $ipAddress) {
24
            $createTag->addChild('ip_address', $ipAddress);
25
        }
26
27
        if ('' !== $description) {
28
            $createTag->addChild('description', $description);
29
        }
30
31
        $response = $this->_client->request($packet);
32
33
        return (string) $response->key;
34
    }
35
36
    /**
37
     * @param string $keyId
38
     *
39
     * @return bool
40
     */
41
    public function delete($keyId)
42
    {
43
        return $this->_delete('key', $keyId, 'delete');
44
    }
45
46
    /**
47
     * @param string $keyId
48
     *
49
     * @return Struct\Info
50
     */
51
    public function get($keyId)
52
    {
53
        $items = $this->_get($keyId);
54
55
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\SecretKey\Info|false adds false to the return on line 55 which is incompatible with the return type documented by PleskX\Api\Operator\SecretKey::get of type PleskX\Api\Struct\SecretKey\Info. It seems like you forgot to handle an error condition.
Loading history...
56
    }
57
58
    /**
59
     * @return Struct\Info[]
60
     */
61
    public function getAll()
62
    {
63
        return $this->_get();
64
    }
65
66
    /**
67
     * @param string|null $keyId
68
     *
69
     * @return Struct\Info[]
70
     */
71 View Code Duplication
    public function _get($keyId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $packet = $this->_client->getPacket();
74
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get_info');
75
76
        $filterTag = $getTag->addChild('filter');
77
        if (!is_null($keyId)) {
78
            $filterTag->addChild('key', $keyId);
79
        }
80
81
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
82
83
        $items = [];
84
        foreach ($response->xpath('//result/key_info') as $keyInfo) {
85
            $items[] = new Struct\Info($keyInfo);
86
        }
87
88
        return $items;
89
    }
90
}
91