NatRule::setId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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
     * @var string
57
     */
58
    private $sourcePrefix = '';
59
60
	/**
61
	 * NatRule constructor.
62
	 */
63
	public function __construct() {
64
	}
65
66
	/**
67
	 * @return string
68
	 */
69
	public function getId() : string {
70
		return $this->id;
71
	}
72
73
	/**
74
	 * @return boolean
75
	 */
76
	public function getEnable() : bool {
77
		return $this->enable;
78
	}
79
80
	/**
81
	 * @param string $id
82
	 * @return void
83
	 */
84
	public function setId(string $id) {
85
		$this->id = str_replace(self::ORIGIN.'_', '', $id);
86
	}
87
	/**
88
	 * @param boolean $enabled
89
	 * @return void
90
	 */
91
	public function setEnable(bool $enabled) {
92
		$this->enable = $enabled;
93
	}
94
	/**
95
	 * @param string $description
96
	 * @return void
97
	 */
98
	public function setDescription(string $description) {
99
		$this->description = $description;
100
	}
101
	/**
102
	 * @param string $destinationIPAddress
103
	 * @return void
104
	 */
105
	public function setDestinationIPAddress(string $destinationIPAddress) {
106
		$this->destinationIPAddress = $destinationIPAddress;
107
	}
108
	/**
109
	 * @param integer $externalPort
110
	 * @return void
111
	 */
112
	public function setExternalPort(int $externalPort) {
113
		$this->externalPort = $externalPort;
114
	}
115
	/**
116
	 * @param integer $internalPort
117
	 * @return void
118
	 */
119
	public function setInternalPort(int $internalPort) {
120
		$this->internalPort = $internalPort;
121
	}
122
	/**
123
	 * @param string $origin
124
	 * @return void
125
	 */
126
	public function setOrigin(string $origin) {
127
		$this->origin = $origin;
128
	}
129
	/**
130
	 * @param boolean $persistent
131
	 * @return void
132
	 */
133
	public function setPersistent(bool $persistent) {
134
		$this->persistent = $persistent;
135
	}
136
	/**
137
	 * Set protocol
138
	 *
139
	 * @param integer $protocol
140
	 * @return void
141
	 */
142
	public function setProtocol(int $protocol) {
143
		if (self::PROTOCOL_BOTH_INTERNAL === $protocol) {
144
			$protocol = self::PROTOCOL_BOTH;
145
		}
146
		$this->protocol = $protocol;
147
	}
148
	/**
149
	 * Set source interface
150
	 *
151
	 * @param string $sourceInterface
152
	 * @return void
153
	 */
154
	public function setSourceInterface(string $sourceInterface) {
155
		$this->sourceInterface = $sourceInterface;
156
	}
157
158
    /**
159
     * @return string
160
     */
161
    public function getSourcePrefix(): ?string
162
    {
163
        return $this->sourcePrefix;
164
    }
165
166
    /**
167
     * @param string $sourcePrefix
168
     */
169
    public function setSourcePrefix(?string $sourcePrefix = null): void
170
    {
171
        $this->sourcePrefix = $sourcePrefix;
172
    }
173
174
175
176
	/**
177
	 * Get the output for create rule
178
	 *
179
	 * @return array
180
	 */
181
	public function getOutput() : array {
182
		$fieldsOutput = [
183
			'id',
184
			'enable',
185
			'description',
186
			'destinationIPAddress',
187
			'externalPort',
188
			'internalPort',
189
			'origin',
190
			'persistent',
191
			'protocol',
192
			'sourceInterface',
193
            'sourcePrefix'
194
		];
195
		return $this->buildOutput($fieldsOutput);
196
	}
197
198
	/**
199
	 * Get the output for delete rule
200
	 *
201
	 * @return array
202
	 */
203
	public function getOutputForDelete() : array {
204
		$fieldsOutput = [
205
			'id',
206
			'destinationIPAddress',
207
			'origin'
208
		];
209
		return $this->buildOutput($fieldsOutput);
210
	}
211
212
	/**
213
	 * Build array output from needed fields
214
	 *
215
	 * @param array $fieldsOutput
216
	 * @return array
217
	 */
218
	protected function buildOutput(array $fieldsOutput) : array {
219
        $output = [];
220
        foreach ($fieldsOutput as $field) {
221
            $method = 'get'.ucfirst($field);
222
            $output[$field] = method_exists($this, $method) ? $this->$method() : $this->$field;
223
        }
224
        return $output;
225
    }
226
227
	/**
228
	 * Build object from command output
229
	 *
230
	 * @param object $input
231
	 * @return NatRule
232
	 */
233
	public static function buildFrom($input) {
234
		$natRule = new self();
235
		$natRule->setId($input->Id);
236
		$natRule->setEnable($input->Enable);
237
		$natRule->setDescription($input->Description);
238
		$natRule->setDestinationIPAddress($input->DestinationIPAddress);
239
		$natRule->setExternalPort((int) $input->ExternalPort);
240
		$natRule->setInternalPort((int) $input->InternalPort);
241
		$natRule->setOrigin($input->Origin);
242
		$natRule->setPersistent((bool) $input->Persistent);
243
		$natRule->setProtocol((int) $input->Protocol);
244
		$natRule->setSourceInterface($input->SourceInterface);
245
		$natRule->setSourcePrefix($input->sourcePrefix);
246
		return $natRule;
247
	}
248
249
}