From 070b980003ee3feea885ca4a0a32ef0817a8a833 Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Sun, 31 Jan 2021 18:11:37 +0300 Subject: [PATCH] Use Buffer in Producer::mpub --- src/Producer.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Producer.php b/src/Producer.php index a226fb2..6eb7b0e 100644 --- a/src/Producer.php +++ b/src/Producer.php @@ -4,9 +4,7 @@ declare(strict_types=1); namespace Nsq; -use function array_map; -use function implode; -use function pack; +use PHPinnacle\Buffer\ByteBuffer; /** * @psalm-suppress PropertyNotSetInConstructor @@ -19,19 +17,21 @@ final class Producer extends Connection } /** - * @psalm-param array $bodies - * - * @psalm-suppress PossiblyFalseOperand + * @psalm-param array $bodies */ 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 { - return pack('N', \strlen($body)).$body; - }, $bodies)); + $buffer->appendUint32(\count($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