FirewallGroup::getById()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 1
nop 1
1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
11
namespace Vultr\Api;
12
13
use Vultr\Entity\FirewallGroup as FirewallGroupEntity;
14
use Vultr\Exceptions\EntityNotFoundException;
15
16
/**
17
 * @author Albert Leitato <[email protected]>
18
 */
19
class FirewallGroup extends AbstractApi
20
{
21
    /**
22
     * List all firewall groups on the current account.
23
     *
24
     * @return FirewallGroupEntity
25
     */
26
    public function list()
27
    {
28
        $response = $this->getAny();
29
30
        return $this->handleResponse($response, self::class, true);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->handleResponse($r...se, self::class, true); of type array|object adds the type array to the return on line 30 which is incompatible with the return type documented by Vultr\Api\FirewallGroup::list of type Vultr\Entity\FirewallGroup.
Loading history...
31
    }
32
33
    /**
34
     * Get a firewall group by its id.
35
     *
36
     * @param string $groupId ID for the firewall group
37
     *
38
     * @return FirewallGroupEntity
39
     *
40
     * @throws EntityNotFoundException
41
     **/
42
    public function getById($groupId)
43
    {
44
        $response = $this->getAny($groupId);
45
        $object = \json_decode($response);
46
        try {
47
            $group = $object->$groupId;
48
        } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...on($e->getMessage()); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
49
            throw new EntityNotFoundException($e->getMessage());
50
        }
51
52
        return new FirewallGroupEntity($group);
53
    }
54
55
    /**
56
     * Get a firewall group by its id.
57
     *
58
     * @param string $groupId ID for the firewall group
59
     *
60
     * @return string API response
61
     */
62
    protected function getAny($groupId = null)
63
    {
64
        $params = null !== $groupId ? '?FIREWALLGROUPID=%s' : '';
65
66
        return $this->adapter->get(\sprintf('%s/firewall/group_list'.$params, $this->endpoint, $groupId));
67
    }
68
69
    /**
70
     * Create a new firewall group on the current account.
71
     *
72
     * @param string $description Description of firewall group
73
     *
74
     * @return FirewallGroupEntity
75
     **/
76
    public function create($description = null)
77
    {
78
        $response = $this->adapter->post(sprintf('%s/firewall/group_create', $this->endpoint), http_build_query(compact('description')));
79
        $object = json_decode($response);
80
81
        return $this->getById($object->FIREWALLGROUPID);
82
    }
83
84
    /**
85
     * Change the description on a firewall group.
86
     *
87
     * @param string $groupId     Firewall group to update
88
     * @param string $description Description of firewall group
89
     *
90
     * @throws HttpException
91
     */
92 View Code Duplication
    public function setDescription($groupId, $description)
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...
93
    {
94
        $data = [
95
            'FIREWALLGROUPID' => $groupId,
96
            'description' => $description,
97
        ];
98
        $this->adapter->post(\sprintf('%s/firewall/group_set_description', $this->endpoint), $data);
99
    }
100
101
    /**
102
     * Delete a firewall group.
103
     *
104
     * Use this function with caution because the firewall group being deleted
105
     * will be detached from all servers. This can result in open ports accessible
106
     * to the internet
107
     *
108
     * @param string $groupId Firewall group to delete
109
     *
110
     * @throws HttpException
111
     */
112
    public function delete($groupId)
113
    {
114
        $data = [
115
            'FIREWALLGROUPID' => $groupId,
116
        ];
117
        $this->adapter->post(\sprintf('%s/firewall/group_delete', $this->endpoint), $data);
118
    }
119
}
120