Passed
Push — master ( 725e32...761ac2 )
by Nicolas
01:39
created

NatRule   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
c 1
b 0
f 0
dl 0
loc 217
rs 10
wmc 20

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setDescription() 0 2 1
A getOutput() 0 14 1
A getId() 0 2 1
A setEnable() 0 2 1
A getOutputForDelete() 0 7 1
A setOrigin() 0 2 1
A getEnable() 0 2 1
A buildOutput() 0 7 3
A setProtocol() 0 5 2
A setPersistent() 0 2 1
A setSourceInterface() 0 2 1
A buildFrom() 0 13 1
A setInternalPort() 0 2 1
A setId() 0 2 1
A setDestinationIPAddress() 0 2 1
A setExternalPort() 0 2 1
A __construct() 0 1 1
1
<?php
2
3
namespace Devgiants\Model;
4
5
class NatRule {
6
7
	const ORIGIN = 'webui';
8
9
	const PROTOCOL_TCP = 6;
10
	const PROTOCOL_UDP = 17;
11
	const PROTOCOL_BOTH = '6,17';
12
	const PROTOCOL_BOTH_INTERNAL = 1000;
13
14
	/**
15
	 * @var string
16
	 */
17
	private $id;
18
	/**
19
	 * @var boolean
20
	 */
21
	private $enable = true;
22
	/**
23
	 * @var string
24
	 */
25
	private $description;
26
	/**
27
	 * @var string
28
	 */
29
	private $destinationIPAddress;
30
	/**
31
	 * @var string
32
	 */
33
	private $externalPort;
34
	/**
35
	 * @var string
36
	 */
37
	private $internalPort;
38
	/**
39
	 * @var string
40
	 */
41
	private $origin = self::ORIGIN;
42
	/**
43
	 * @var boolean
44
	 */
45
	private $persistent = true;
46
	/**
47
	 * @var int|string
48
	 */
49
	private $protocol = self::PROTOCOL_TCP;
50
	/**
51
	 * @var string
52
	 */
53
	private $sourceInterface = 'data';
54
55
	/**
56
	 * NatRule constructor.
57
	 */
58
	public function __construct() {
59
	}
60
61
	/**
62
	 * @return string
63
	 */
64
	public function getId() : string {
65
		return $this->id;
66
	}
67
68
	/**
69
	 * @return boolean
70
	 */
71
	public function getEnable() : bool {
72
		return $this->enable;
73
	}
74
75
	/**
76
	 * @param string $id
77
	 * @return void
78
	 */
79
	public function setId(string $id) {
80
		$this->id = str_replace(self::ORIGIN.'_', '', $id);
81
	}
82
	/**
83
	 * @param boolean $enabled
84
	 * @return void
85
	 */
86
	public function setEnable(bool $enabled) {
87
		$this->enable = $enabled;
88
	}
89
	/**
90
	 * @param string $description
91
	 * @return void
92
	 */
93
	public function setDescription(string $description) {
94
		$this->description = $description;
95
	}
96
	/**
97
	 * @param string $destinationIPAddress
98
	 * @return void
99
	 */
100
	public function setDestinationIPAddress(string $destinationIPAddress) {
101
		$this->destinationIPAddress = $destinationIPAddress;
102
	}
103
	/**
104
	 * @param integer $externalPort
105
	 * @return void
106
	 */
107
	public function setExternalPort(int $externalPort) {
108
		$this->externalPort = $externalPort;
109
	}
110
	/**
111
	 * @param integer $internalPort
112
	 * @return void
113
	 */
114
	public function setInternalPort(int $internalPort) {
115
		$this->internalPort = $internalPort;
116
	}
117
	/**
118
	 * @param string $origin
119
	 * @return void
120
	 */
121
	public function setOrigin(string $origin) {
122
		$this->origin = $origin;
123
	}
124
	/**
125
	 * @param boolean $persistent
126
	 * @return void
127
	 */
128
	public function setPersistent(bool $persistent) {
129
		$this->persistent = $persistent;
130
	}
131
	/**
132
	 * Set protocol
133
	 *
134
	 * @param integer $protocol
135
	 * @return void
136
	 */
137
	public function setProtocol(int $protocol) {
138
		if (self::PROTOCOL_BOTH_INTERNAL === $protocol) {
139
			$protocol = self::PROTOCOL_BOTH;
140
		}
141
		$this->protocol = $protocol;
142
	}
143
	/**
144
	 * Set source interface
145
	 *
146
	 * @param string $sourceInterface
147
	 * @return void
148
	 */
149
	public function setSourceInterface(string $sourceInterface) {
150
		$this->sourceInterface = $sourceInterface;
151
	}
152
153
	/**
154
	 * Get the output for create rule
155
	 *
156
	 * @return array
157
	 */
158
	public function getOutput() : array {
159
		$fieldsOutput = [
160
			'id',
161
			'enable',
162
			'description',
163
			'destinationIPAddress',
164
			'externalPort',
165
			'internalPort',
166
			'origin',
167
			'persistent',
168
			'protocol',
169
			'sourceInterface'
170
		];
171
		return $this->buildOutput($fieldsOutput);
172
	}
173
174
	/**
175
	 * Get the output for delete rule
176
	 *
177
	 * @return array
178
	 */
179
	public function getOutputForDelete() : array {
180
		$fieldsOutput = [
181
			'id',
182
			'destinationIPAddress',
183
			'origin'
184
		];
185
		return $this->buildOutput($fieldsOutput);
186
	}
187
188
	/**
189
	 * Build array output from needed fields
190
	 *
191
	 * @param array $fieldsOutput
192
	 * @return array
193
	 */
194
	protected function buildOutput(array $fieldsOutput) : array {
195
        $output = [];
196
        foreach ($fieldsOutput as $field) {
197
            $method = 'get'.ucfirst($field);
198
            $output[$field] = method_exists($this, $method) ? $this->$method() : $this->$field;
199
        }
200
        return $output;
201
    }
202
203
	/**
204
	 * Build object from command output
205
	 *
206
	 * @param object $input
207
	 * @return NatRule
208
	 */
209
	public static function buildFrom($input) {
210
		$natRule = new self();
211
		$natRule->setId($input->Id);
212
		$natRule->setEnable($input->Enable);
213
		$natRule->setDescription($input->Description);
214
		$natRule->setDestinationIPAddress($input->DestinationIPAddress);
215
		$natRule->setExternalPort((int) $input->ExternalPort);
216
		$natRule->setInternalPort((int) $input->InternalPort);
217
		$natRule->setOrigin($input->Origin);
218
		$natRule->setPersistent((bool) $input->Persistent);
219
		$natRule->setProtocol((int) $input->Protocol);
220
		$natRule->setSourceInterface($input->SourceInterface);
221
		return $natRule;
222
	}
223
224
}