NetworkSettings::withConnectionTimeout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Settings;
6
7
use BitWasp\Bitcoin\Networking\DnsSeeds\DnsSeedList;
8
9
abstract class NetworkSettings implements NetworkSettingsInterface, MutableNetworkSettingsInterface
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $defaultP2PPort = 8333;
15
16
    /**
17
     * @var int
18
     */
19
    protected $connectionTimeout = 10;
20
21
    /**
22
     * @var string|null
23
     */
24
    protected $dnsServer = '8.8.8.8';
25
26
    /**
27
     * @var DnsSeedList|null
28
     */
29
    protected $dnsSeeds = null;
30
31
    /**
32
     * @var int
33
     */
34
    protected $maxRetries = 5;
35
36
    /**
37
     * @return DnsSeedList
38
     */
39
    public function getDnsSeedList(): DnsSeedList
40
    {
41
        if (null === $this->dnsSeeds) {
42
            throw new \RuntimeException("Missing DNS seed list");
43
        }
44
        return $this->dnsSeeds;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getDnsServer(): string
51
    {
52
        if (null === $this->dnsServer) {
53
            throw new \RuntimeException("Missing DNS server");
54
        }
55
        return $this->dnsServer;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getDefaultP2PPort(): int
62
    {
63
        return $this->defaultP2PPort;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getConnectionTimeout(): int
70
    {
71
        return $this->connectionTimeout;
72
    }
73
74
    /**
75
     * @return int
76
     */
77
    public function getMaxConnectRetries(): int
78
    {
79
        return $this->maxRetries;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getSocketParams(): array
86
    {
87
        return [
88
            'timeout' => $this->getConnectionTimeout(),
89
        ];
90
    }
91
92
    /**
93
     * @param string $server
94
     * @return $this
95
     */
96
    public function withDnsServer(string $server = null): NetworkSettings
97
    {
98
        $clone = clone $this;
99
        $clone->dnsServer = $server;
100
        return $clone;
101
    }
102
103
    /**
104
     * @param DnsSeedList $list
105
     * @return $this
106
     */
107
    public function withDnsSeeds(DnsSeedList $list = null): NetworkSettings
108
    {
109
        $clone = clone $this;
110
        $clone->dnsSeeds = $list;
111
        return $clone;
112
    }
113
114
    /**
115
     * @param int $p2pPort
116
     * @return $this
117
     */
118
    public function withDefaultP2PPort(int $p2pPort): NetworkSettings
119
    {
120
        $clone = clone $this;
121
        $clone->defaultP2PPort = $p2pPort;
122
        return $clone;
123
    }
124
125
    /**
126
     * @param int $timeout
127
     * @return $this
128
     */
129
    public function withConnectionTimeout(int $timeout): NetworkSettings
130
    {
131
        $clone = clone $this;
132
        $clone->connectionTimeout = $timeout;
133
        return $clone;
134
    }
135
136
    /**
137
     * @param int $maxRetries
138
     * @return $this
139
     */
140
    public function withMaxConnectRetries(int $maxRetries): NetworkSettings
141
    {
142
        $clone = clone $this;
143
        $clone->maxRetries = $maxRetries;
144
        return $clone;
145
    }
146
}
147