Use Buffer in Producer::mpub

This commit is contained in:
2021-01-31 18:11:37 +03:00
parent a66f622cf6
commit 070b980003

View File

@@ -4,9 +4,7 @@ declare(strict_types=1);
namespace Nsq; namespace Nsq;
use function array_map; use PHPinnacle\Buffer\ByteBuffer;
use function implode;
use function pack;
/** /**
* @psalm-suppress PropertyNotSetInConstructor * @psalm-suppress PropertyNotSetInConstructor
@@ -19,19 +17,21 @@ final class Producer extends Connection
} }
/** /**
* @psalm-param array<mixed, mixed> $bodies * @psalm-param array<int, mixed> $bodies
*
* @psalm-suppress PossiblyFalseOperand
*/ */
public function mpub(string $topic, array $bodies): void public function mpub(string $topic, array $bodies): void
{ {
$num = pack('N', \count($bodies)); static $buffer;
$buffer ??= new ByteBuffer();
$mb = implode('', array_map(static function ($body): string { $buffer->appendUint32(\count($bodies));
return pack('N', \strlen($body)).$body;
}, $bodies));
$this->command('MPUB', $topic, $num.$mb)->checkIsOK(); foreach ($bodies as $body) {
$buffer->appendUint32(\strlen($body));
$buffer->append($body);
}
$this->command('MPUB', $topic, $buffer->flush())->checkIsOK();
} }
public function dpub(string $topic, string $body, int $delay): void public function dpub(string $topic, string $body, int $delay): void