seria/src/Torrents/TorrentPieces.php

40 lines
1.2 KiB
PHP

<?php
namespace Seria\Torrents;
use InvalidArgumentException;
use RuntimeException;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class TorrentPieces {
private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) {
$this->cache = new DbStatementCache($dbConn);
}
public function getPieces(TorrentInfo|string $torrentInfo): array {
$stmt = $this->cache->get('SELECT piece_id, torrent_id, piece_hash FROM ser_torrents_pieces WHERE torrent_id = ? ORDER BY piece_id ASC');
$stmt->addParameter(1, $torrentInfo instanceof TorrentInfo ? $torrentInfo->getId() : $torrentInfo);
$stmt->execute();
$result = $stmt->getResult();
$pieces = [];
while($result->next())
$pieces[] = new TorrentPieceInfo($result);
return $pieces;
}
public function createPiece(
TorrentInfo|string $torrentInfo,
string $hash
): void {
$stmt = $this->cache->get('INSERT INTO ser_torrents_pieces (torrent_id, piece_hash) VALUES (?, ?)');
$stmt->addParameter(1, $torrentInfo instanceof TorrentInfo ? $torrentInfo->getId() : $torrentInfo);
$stmt->addParameter(2, $hash);
$stmt->execute();
}
}