Passed
Push — 2.0-dev ( 2f3546...5e7c8b )
by Takehiro
05:06 queued 01:02
created

Configuration::checkAccessSocketPath()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 3
nc 2
nop 0
crap 4
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
        } else {
70 1
            throw new InvalidArgumentException("The option '$property' is not recognised.");
71
        }
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
            if (property_exists($this, $key)) {
83 8
                $this->{$key} = $value;
84 8
            } else {
85 1
                throw new InvalidArgumentException("The option '$key' is not recognised.");
86
            }
87 8
        }
88 7
    }
89
90
    /**
91
     * プロパティの値の妥当性を確認
92
     */
93 7
    private function validateProperty()
94
    {
95 7
        switch ($this->socketType) {
96 7
            case "unix":
97 3
                $this->validatePropetrySocketPath();
98 2
                $this->checkAccessSocketPath();
99 1
                break;
100 4
            case "tcp":
101 3
                $this->validatePropetrySocketAddress();
102 2
                $this->validatePropetrySocketPort();
103 1
                break;
104 1
            default:
105 1
                throw new InvalidArgumentException("Socket Type is invalid. Must be one of 'unix' or 'tcp'.");
106 3
        }
107 2
    }
108
109
    /**
110
     * socketPathの設定確認
111
     */
112 3
    private function validatePropetrySocketPath()
113
    {
114 3
        if (strlen($this->socketPath) === 0) {
115 1
            throw new InvalidArgumentException("The option socketPath must be supplied for socketType 'unix'.");
116
        }
117 2
    }
118
119
    /**
120
     * socketPathが利用可能かの確認
121
     */
122 2
    private function checkAccessSocketPath()
123
    {
124 2
        if (!file_exists($this->socketPath) || !is_readable($this->socketPath) || !is_writable($this->socketPath)) {
125 1
            throw new InvalidArgumentException("The supplied socketPath '{$this->socketPath}' is not accessible to this script.");
126
        }
127 1
    }
128
129
    /**
130
     * socketAddressの設定確認
131
     */
132 3
    private function validatePropetrySocketAddress()
133
    {
134 3
        if (strlen($this->socketAddress) === 0) {
135 1
            throw new InvalidArgumentException("The option socketAddress must be supplied for socketType 'tcp'.");
136
        }
137 2
    }
138
139
    /**
140
     * socketPortの設定確認
141
     */
142 2
    private function validatePropetrySocketPort()
143
    {
144 2
        if (strlen($this->socketPort) === 0) {
145 1
            throw new InvalidArgumentException("The option socketPort must be supplied for socketType 'tcp'.");
146
        }
147 1
    }
148
}
149