/ src / Network / NetworkResult.php
NetworkResult.php
  1  <?php
  2  
  3  namespace BitcoindRPC\Network;
  4  
  5  use BitcoindRPC\RpcResponse;
  6  
  7  class NetworkResult
  8  {
  9      private RpcResponse $response;
 10  
 11      public function __construct(RpcResponse $response)
 12      {
 13          $this->response = $response;
 14      }
 15  
 16      public function isSuccess(): bool
 17      {
 18          return $this->response->isSuccess();
 19      }
 20  
 21      public function getData(): mixed
 22      {
 23          return $this->response->getData();
 24      }
 25  
 26      public function getError(): ?string
 27      {
 28          return $this->response->getError();
 29      }
 30  
 31      public function getStatusCode(): int
 32      {
 33          return $this->response->getStatusCode();
 34      }
 35  
 36      public function toArray(): array
 37      {
 38          return $this->response->toArray();
 39      }
 40  
 41      public function getNetworkInfo(): ?array
 42      {
 43          $data = $this->getData();
 44          return is_array($data) ? $data : null;
 45      }
 46  
 47      public function getPeerInfo(): ?array
 48      {
 49          $data = $this->getData();
 50          return is_array($data) ? $data : null;
 51      }
 52  
 53      public function getConnectionCount(): ?int
 54      {
 55          $data = $this->getData();
 56          return is_numeric($data) ? (int) $data : null;
 57      }
 58  
 59      public function getNetTotals(): ?array
 60      {
 61          $data = $this->getData();
 62          return is_array($data) ? $data : null;
 63      }
 64  
 65      public function getBannedList(): ?array
 66      {
 67          $data = $this->getData();
 68          return is_array($data) ? $data : null;
 69      }
 70  
 71      public function getNodeAddresses(): ?array
 72      {
 73          $data = $this->getData();
 74          return is_array($data) ? $data : null;
 75      }
 76  
 77      public function getAddedNodes(): ?array
 78      {
 79          $data = $this->getData();
 80          return is_array($data) ? $data : null;
 81      }
 82  
 83      // Network info specific methods
 84      public function getNetworkVersion(): ?int
 85      {
 86          $info = $this->getNetworkInfo();
 87          return $info['version'] ?? null;
 88      }
 89  
 90      public function getNetworkSubversion(): ?string
 91      {
 92          $info = $this->getNetworkInfo();
 93          return $info['subversion'] ?? null;
 94      }
 95  
 96      public function getProtocolVersion(): ?int
 97      {
 98          $info = $this->getNetworkInfo();
 99          return $info['protocolversion'] ?? null;
100      }
101  
102      public function isNetworkActive(): ?bool
103      {
104          $info = $this->getNetworkInfo();
105          return $info['networkactive'] ?? null;
106      }
107  
108      public function getLocalServices(): ?string
109      {
110          $info = $this->getNetworkInfo();
111          return $info['localservices'] ?? null;
112      }
113  
114      // Peer info specific methods
115      public function getConnectedPeers(): ?array
116      {
117          $peers = $this->getPeerInfo();
118          if (!$peers) {
119              return null;
120          }
121  
122          $connected = [];
123          foreach ($peers as $peer) {
124              if (($peer['connected'] ?? false) === true) {
125                  $connected[] = $peer;
126              }
127          }
128  
129          return $connected;
130      }
131  
132      public function getPeerCount(): ?int
133      {
134          $peers = $this->getPeerInfo();
135          return is_array($peers) ? count($peers) : null;
136      }
137  
138      public function getConnectedPeerCount(): ?int
139      {
140          $connected = $this->getConnectedPeers();
141          return is_array($connected) ? count($connected) : null;
142      }
143  
144      // Net totals specific methods
145      public function getTotalBytesReceived(): ?int
146      {
147          $totals = $this->getNetTotals();
148          return $totals['totalbytesrecv'] ?? null;
149      }
150  
151      public function getTotalBytesSent(): ?int
152      {
153          $totals = $this->getNetTotals();
154          return $totals['totalbytessent'] ?? null;
155      }
156  
157      public function getTimeMillis(): ?int
158      {
159          $totals = $this->getNetTotals();
160          return $totals['timemillis'] ?? null;
161      }
162  
163      // Banned list specific methods
164      public function getBannedCount(): ?int
165      {
166          $banned = $this->getBannedList();
167          return is_array($banned) ? count($banned) : null;
168      }
169  
170      // Node addresses specific methods
171      public function getNodeAddressCount(): ?int
172      {
173          $addresses = $this->getNodeAddresses();
174          return is_array($addresses) ? count($addresses) : null;
175      }
176  
177      public function getNetworkSummary(): array
178      {
179          $summary = [
180              'success' => $this->isSuccess(),
181              'error' => $this->getError(),
182              'status_code' => $this->getStatusCode(),
183              'type' => 'unknown'
184          ];
185  
186          if ($this->isSuccess()) {
187              $data = $this->getData();
188              
189              if (is_array($data)) {
190                  // Network info response
191                  if (isset($data['version'])) {
192                      $summary['type'] = 'network_info';
193                      $summary['version'] = $data['version'];
194                      $summary['subversion'] = $data['subversion'] ?? null;
195                      $summary['protocol_version'] = $data['protocolversion'] ?? null;
196                      $summary['connections'] = $data['connections'] ?? null;
197                  }
198                  // Peer info response
199                  elseif (isset($data[0]) && is_array($data[0]) && isset($data[0]['addr'])) {
200                      $summary['type'] = 'peer_info';
201                      $summary['peer_count'] = count($data);
202                      $summary['connected_peers'] = $this->getConnectedPeerCount();
203                  }
204                  // Net totals response
205                  elseif (isset($data['totalbytesrecv'])) {
206                      $summary['type'] = 'net_totals';
207                      $summary['bytes_received'] = $data['totalbytesrecv'];
208                      $summary['bytes_sent'] = $data['totalbytessent'];
209                  }
210                  // Banned list response
211                  elseif (isset($data[0]) && is_array($data[0]) && isset($data[0]['address'])) {
212                      $summary['type'] = 'banned_list';
213                      $summary['banned_count'] = count($data);
214                  }
215                  // Added node info response
216                  elseif (isset($data[0]) && is_array($data[0]) && isset($data[0]['addednode'])) {
217                      $summary['type'] = 'added_nodes';
218                      $summary['node_count'] = count($data);
219                  }
220                  // Node addresses response
221                  elseif (isset($data[0]) && is_array($data[0]) && isset($data[0]['address'])) {
222                      $summary['type'] = 'node_addresses';
223                      $summary['address_count'] = count($data);
224                  }
225              } elseif (is_numeric($data)) {
226                  // Connection count response
227                  $summary['type'] = 'connection_count';
228                  $summary['connections'] = (int) $data;
229              } elseif ($data === null) {
230                  // Success with no data (like clearbanned, setnetworkactive)
231                  $summary['type'] = 'operation_success';
232              }
233          }
234  
235          return $summary;
236      }
237  
238      public function getPeerVersions(): ?array
239      {
240          $peers = $this->getPeerInfo();
241          if (!$peers) {
242              return null;
243          }
244  
245          $versions = [];
246          foreach ($peers as $peer) {
247              $version = $peer['version'] ?? 'unknown';
248              $versions[$version] = ($versions[$version] ?? 0) + 1;
249          }
250  
251          return $versions;
252      }
253  
254      public function getPeerCountries(): ?array
255      {
256          $peers = $this->getPeerInfo();
257          if (!$peers) {
258              return null;
259          }
260  
261          $countries = [];
262          foreach ($peers as $peer) {
263              $address = $peer['addr'] ?? '';
264              // Simplified country extraction - in real implementation use geoip
265              $country = 'unknown';
266              
267              if (preg_match('/\d+\.\d+\.\d+\.\d+/', $address)) {
268                  $country = 'IPv4';
269              } elseif (str_contains($address, '[')) {
270                  $country = 'IPv6';
271              }
272              
273              $countries[$country] = ($countries[$country] ?? 0) + 1;
274          }
275  
276          return $countries;
277      }
278  }