Completed
Pull Request — master (#85)
by thomas
02:23
created

NetworkSettings::getSocketParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Settings;
4
5
use BitWasp\Bitcoin\Networking\DnsSeeds\DnsSeedList;
6
7
abstract class NetworkSettings
8
{
9
    /**
10
     * @var int
11
     */
12
    private $defaultP2PPort = 8333;
13
14
    /**
15
     * @var int
16
     */
17
    private $connectionTimeout = 10;
18
19
    /**
20
     * @var string
21
     */
22
    private $dnsServer = '8.8.8.8';
23
24
    /**
25
     * @var DnsSeedList
26
     */
27
    private $dnsSeeds = null;
28
29
    /**
30
     * @var int
31
     */
32
    private $maxRetries = 5;
33
34 24
    public function __construct()
35
    {
36 24
        $this->setup();
37 24
        $this->validateSettings();
38 24
    }
39
40
    /**
41
     * Setup of network goes here:
42
     */
43
    abstract protected function setup();
44
45
    /**
46
     *
47
     */
48 24
    protected function validateSettings()
49
    {
50 24
        if (!$this->dnsSeeds instanceof DnsSeedList) {
51
            throw new \RuntimeException("Invalid DNS Seeds");
52
        }
53
54 24
        if (!(is_integer($this->maxRetries) && $this->maxRetries >= 0)) {
55
            throw new \RuntimeException("Max retries must be a positive integer");
56
        }
57
58 24
        if (!(is_integer($this->connectionTimeout) && $this->connectionTimeout >= 0)) {
59
            throw new \RuntimeException("Connection timeout must be a positive integer");
60
        }
61
62 24
        if (!(is_integer($this->defaultP2PPort) && $this->defaultP2PPort >= 0)) {
63
            throw new \RuntimeException("Default P2P must be a positive integer");
64
        }
65 24
    }
66
67
    /**
68
     * @return DnsSeedList
69
     */
70 12
    public function getDnsSeedList()
71
    {
72 12
        return $this->dnsSeeds;
73
    }
74
75
    /**
76
     * @param DnsSeedList $list
77
     * @return $this
78
     */
79 24
    public function setDnsSeeds(DnsSeedList $list)
80
    {
81 24
        $this->dnsSeeds = $list;
82 24
        return $this;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 15
    public function getDnsServer()
89
    {
90 15
        return $this->dnsServer;
91
    }
92
93
    /**
94
     * @param string $server
95
     * @return $this
96
     */
97
    public function setDnsServer($server)
98
    {
99
        $this->dnsServer = $server;
100
        return $this;
101
    }
102
103
    /**
104
     * @return int
105
     */
106 6
    public function getDefaultP2PPort()
107
    {
108 6
        return $this->defaultP2PPort;
109
    }
110
111
    /**
112
     * @param int $port
113
     * @return $this
114
     */
115 24
    public function setDefaultP2PPort($port)
116
    {
117 24
        $this->defaultP2PPort = $port;
118 24
        return $this;
119
    }
120
121
    /**
122
     * @return int
123
     */
124 12
    public function getConnectionTimeout()
125
    {
126 12
        return $this->connectionTimeout;
127
    }
128
129
    /**
130
     * @param int $timeout
131
     * @return $this
132
     */
133
    public function setConnectionTimeout($timeout)
134
    {
135
        $this->connectionTimeout = $timeout;
136
        return $this;
137
    }
138
139
    /**
140
     * @param int $maxRetries
141
     * @return $this
142
     */
143 3
    public function setMaxConnectRetries($maxRetries)
144
    {
145 3
        $this->maxRetries = $maxRetries;
146 3
        return $this;
147
    }
148
149
    /**
150
     * @return int
151
     */
152 3
    public function getMaxConnectRetries()
153
    {
154 3
        return $this->maxRetries;
155
    }
156
157
    /**
158
     * @return array
159
     */
160 12
    public function getSocketParams()
161
    {
162
        return [
163 12
            'timeout' => $this->getConnectionTimeout(),
164 4
        ];
165
    }
166
}
167