seria/src/Torrents/TorrentPeerInfo.php

110 lines
2.7 KiB
PHP

<?php
namespace Seria\Torrents;
use Index\Data\IDbResult;
use Seria\Users\UserInfo;
readonly class TorrentPeerInfo {
private string $id;
private string $torrentId;
private ?string $userId;
private string $address;
private int $port;
private int $updated;
private int $expires;
private string $agent;
private string $key;
private int $bytesUploaded;
private int $bytesDownloaded;
private int $bytesLeft;
public function __construct(IDbResult $result) {
$this->id = $result->getString(0);
$this->torrentId = $result->getString(1);
$this->userId = $result->isNull(2) ? null : $result->getString(2);
$this->address = $result->getString(3);
$this->port = $result->getInteger(4);
$this->updated = $result->getInteger(5);
$this->expires = $result->getInteger(6);
$this->agent = $result->getString(7);
$this->key = $result->getString(8);
$this->bytesUploaded = $result->getInteger(9);
$this->bytesDownloaded = $result->getInteger(10);
$this->bytesLeft = $result->getInteger(11);
}
public function getId(): string {
return $this->id;
}
public function getTorrentId(): string {
return $this->torrentId;
}
public function getUserId(): ?string {
return $this->userId;
}
public function hasUserId(): bool {
return $this->userId !== null;
}
public function verifyUser(?UserInfo $userInfo): bool {
return $this->userId === null
? $userInfo === null
: $this->userId === $userInfo->getId();
}
public function getAddress(): string {
return $this->address;
}
public function getAddressRaw(): string {
return inet_pton($this->address);
}
public function getPort(): int {
return $this->port;
}
public function getUpdatedTime(): int {
return $this->updated;
}
public function getExpiresTime(): int {
return $this->expires;
}
public function getAgent(): string {
return $this->agent;
}
public function getKey(): string {
return $this->key;
}
public function getBytesUploaded(): int {
return $this->bytesUploaded;
}
public function getBytesDownloaded(): int {
return $this->bytesDownloaded;
}
public function getBytesRemaining(): int {
return $this->bytesLeft;
}
public function isSeed(): bool {
return $this->bytesLeft <= 0;
}
public function isLeech(): bool {
return $this->bytesLeft > 0;
}
public function verifyKey(string $key): bool {
return hash_equals($this->getKey(), $key);
}
}