AddrTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
set() 0 1 ?
A appendAddr() 0 16 4
B appendHostAttr() 0 25 8
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP;
13
14
use Exception;
15
16
/**
17
 * a IP address trait to give common functionality needed to for addr elements
18
 */
19
trait AddrTrait
20
{
21
    protected $host_attr_index = 0;
22
23
    /**
24
     * Set value to path
25
     *
26
     * @param string $path
27
     * @param mixed $value
28
     *
29
     * @return \DOMElement
30
     */
31
    abstract public function set($path = null, $value = null);
32
33
    protected function appendAddr($path, $ip)
34
    {
35
        // validate IP
36
        $ip_type = Validator::getIPType($ip);
37
        if ($ip_type === false) {
38
            throw new Exception(sprintf('%s is not a valid IP address', $ip));
39
        }
40
41
        $node = $this->set($path, $ip);
42
43
        if ($ip_type === Validator::TYPE_IPV4) {
44
            $node->setAttribute('ip', 'v4');
45
        } elseif ($ip_type === Validator::TYPE_IPV6) {
46
            $node->setAttribute('ip', 'v6');
47
        }
48
    }
49
50
    protected function appendHostAttr($base_path, $host, $ips = null)
51
    {
52
        if (!Validator::isHostname($host)) {
53
            throw new Exception(sprintf('%s is not a valid host name', $host));
54
        }
55
56
        $base_path = sprintf($base_path, $this->host_attr_index);
57
58
        $this->set($base_path . '/domain:hostName', $host);
59
60
        if (!empty($ips) && is_array($ips)) {
61
            foreach ($ips as $ip) {
62
                $ip_type = Validator::getIPType($ip);
63
                if ($ip_type === false) {
64
                    throw new Exception(sprintf('%s is not a valid IP address', $ip));
65
                } elseif ($ip_type === Validator::TYPE_IPV4) {
66
                    $this->set($base_path . '/domain:hostAddr[@ip=\'v4\']', $ip);
67
                } elseif ($ip_type === Validator::TYPE_IPV6) {
68
                    $this->set($base_path . '/domain:hostAddr[@ip=\'v6\']', $ip);
69
                }
70
            }
71
        }
72
73
        ++$this->host_attr_index;
74
    }
75
}
76