FirewallRule   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 8
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 7 1
A create() 0 18 2
A delete() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
namespace Vultr\Api;
11
12
use Vultr\Entity\FirewallRule as FirewallRuleEntity;
13
14
/**
15
 * @author Albert Leitato <[email protected]>
16
 */
17
class FirewallRule extends AbstractApi
18
{
19
    /**
20
     * List all firewall rules on the current account.
21
     *
22
     * @see https://www.vultr.com/api/#firewall_rule_list
23
     *
24
     * @return FirewallRuleEntity
25
     */
26
    public function list($groupId, $direction, $ipType)
27
    {
28
        $params   = 'FIREWALLGROUPID=%s&direction=%s&ip_type=%s';
29
        $response = $this->adapter->get(\sprintf('%s/firewall/rule_list' . $params, $this->endpoint, $groupId, $direction, $ipType));
30
31
        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 31 which is incompatible with the return type documented by Vultr\Api\FirewallRule::list of type Vultr\Entity\FirewallRule.
Loading history...
32
    }
33
34
    /**
35
     * Create a rule in a firewall group.
36
     *
37
     * @param string $groupId    Target firewall group
38
     * @param string $direction  Direction of rule. Possible values: "in".
39
     * @param string $ipType     IP address type. Possible values: "v4", "v6".
40
     * @param string $protocol   Protocol type. Possible
41
     * @param string $subnet     IP address representing a subnet
42
     * @param int    $subnetSize iP prefix size in bits
43
     * @param string $port       tCP/UDP only
44
     *
45
     * @throws HttpException
46
     */
47
    public function create(string $groupId, string $direction, string $ipType, string $protocol, string $subnet, int $subnetSize, $port = null)
48
    {
49
        $content = [
50
            'FIREWALLGROUPID' => $groupId,
51
            'direction'       => $direction,
52
            'ip_type'         => $ipType,
53
            'protocol'        => $protocol,
54
            'subnet'          => $subnet,
55
            'subnet_size'     => $subnetSize,
56
        ];
57
        if (null !== $port) {
58
            $content['port'] = $port;
59
        }
60
61
        $response = $this->adapter->post(\sprintf('%s/firewall/rule_create', $this->endpoint), $content);
62
63
        return \json_decode($response);
64
    }
65
66
    /**
67
     * Delete a rule in a firewall group.
68
     *
69
     * @param string $groupId    Firewall group to delete
70
     * @param int    $ruleNumber Rule number to delete
71
     *
72
     * @throws HttpException
73
     */
74 View Code Duplication
    public function delete($groupId, $ruleNumber)
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...
75
    {
76
        $data = [
77
            'FIREWALLGROUPID' => $groupId,
78
            'rulenumber'      => $ruleNumber,
79
        ];
80
        $this->adapter->post(\sprintf('%s/firewall/rule_delete', $this->endpoint), $data);
81
    }
82
}
83