Configuration::validateProperty()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 14
cts 14
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 0
crap 3
1
<?php
2
namespace Kwkm\MkLiveStatusClient;
3
4
use \InvalidArgumentException;
5
6
/**
7
 * Class Configuration
8
 *
9
 * @package Kwkm\MkLiveStatusClient
10
 * @property-read string $socketType      接続方式(unix or tcp)
11
 * @property-read string $socketPath      ソケットファイル
12
 * @property-read string $socketAddress   TCP接続時のIPアドレス
13
 * @property-read string $socketPort      TCP接続時のポート
14
 * @property-read array $socketTimeout    TCP接続時のタイムアウト秒数
15
 * @author Takehiro Kawakami <[email protected]>
16
 * @license MIT
17
 */
18
class Configuration
19
{
20
    /**
21
     * 接続方式(unix or tcp)
22
     * @var string
23
     */
24
    protected $socketType = "unix";
25
26
    /**
27
     * ソケットファイル
28
     * @var string
29
     */
30
    protected $socketPath = "/var/run/nagios/rw/live";
31
32
    /**
33
     * TCP接続時のIPアドレス
34
     * @var string
35
     */
36
    protected $socketAddress = "";
37
38
    /**
39
     * TCP接続時のポート
40
     * @var string
41
     */
42
    protected $socketPort = "";
43
44
    /**
45
     * TCP接続時のタイムアウト秒数
46
     * @var array
47
     * @link http://php.net/manual/en/function.socket-set-option.php
48
     */
49
    protected $socketTimeout = array();
50
51
    /**
52
     * コンストラクタ
53
     *
54
     * @param array $conf
55
     * @throw \BadFunctionCallException
56
     * @throw \InvalidArgumentException
57
     */
58 8
    public function __construct(array $conf)
59
    {
60 8
        $this->assignProperty($conf);
61
62 7
        $this->validateProperty();
63 2
    }
64
65 1
    public function __get($property)
66
    {
67 1
        if (property_exists($this, $property)) {
68 1
            return $this->{$property};
69
        }
70
71 1
        throw new InvalidArgumentException("The option '$property' is not recognised.");
72
    }
73
74
    /**
75
     * 設定をプロパティに割り当て
76
     *
77
     * @param array $conf
78
     */
79 8
    private function assignProperty($conf)
80
    {
81 8
        foreach ($conf as $key => $value) {
82 8
            $this->setProperty($key, $value);
83 8
        }
84 7
    }
85
86
    /**
87
     * プロパティに値をセット
88
     * @param $property
89
     * @param $value
90
     */
91 8
    private function setProperty($property, $value)
92
    {
93 8
        if (false === property_exists($this, $property)) {
94 1
            throw new InvalidArgumentException("The option '$property' is not recognised.");
95
        }
96
97 8
        $this->{$property} = $value;
98 8
    }
99
100
    /**
101
     * プロパティの値の妥当性を確認
102
     */
103 7
    private function validateProperty()
104
    {
105 7
        switch ($this->socketType) {
106 7
            case "unix":
107 3
                $this->validatePropetrySocketPath();
108 2
                $this->checkAccessSocketPath();
109 1
                break;
110 4
            case "tcp":
111 3
                $this->validatePropetrySocketAddress();
112 2
                $this->validatePropetrySocketPort();
113 1
                break;
114 1
            default:
115 1
                throw new InvalidArgumentException("Socket Type is invalid. Must be one of 'unix' or 'tcp'.");
116 3
        }
117 2
    }
118
119
    /**
120
     * socketPathの設定確認
121
     */
122 3
    private function validatePropetrySocketPath()
123
    {
124 3
        if (strlen($this->socketPath) === 0) {
125 1
            throw new InvalidArgumentException("The option socketPath must be supplied for socketType 'unix'.");
126
        }
127 2
    }
128
129
    /**
130
     * socketPathが利用可能かの確認
131
     */
132 2
    private function checkAccessSocketPath()
133
    {
134 2
        if (!file_exists($this->socketPath) || !is_readable($this->socketPath) || !is_writable($this->socketPath)) {
135 1
            throw new InvalidArgumentException(
136 1
                "The supplied socketPath '{$this->socketPath}' is not accessible to this script."
137 1
            );
138
        }
139 1
    }
140
141
    /**
142
     * socketAddressの設定確認
143
     */
144 3
    private function validatePropetrySocketAddress()
145
    {
146 3
        if (strlen($this->socketAddress) === 0) {
147 1
            throw new InvalidArgumentException("The option socketAddress must be supplied for socketType 'tcp'.");
148
        }
149 2
    }
150
151
    /**
152
     * socketPortの設定確認
153
     */
154 2
    private function validatePropetrySocketPort()
155
    {
156 2
        if (strlen($this->socketPort) === 0) {
157 1
            throw new InvalidArgumentException("The option socketPort must be supplied for socketType 'tcp'.");
158
        }
159 1
    }
160
}
161