Skip to content

Commit

Permalink
declare the incoming method frames directly on the enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Dec 3, 2023
1 parent e14ad85 commit 3ff8c9b
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 456 deletions.
139 changes: 138 additions & 1 deletion src/Transport/Frame/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@

namespace Innmind\AMQP\Transport\Frame;

use Innmind\AMQP\Transport\Frame\Value\{
Unpacked,
Bits,
LongString,
ShortString,
UnsignedLongInteger,
UnsignedLongLongInteger,
UnsignedOctet,
UnsignedShortInteger,
Table,
};
use Innmind\TimeContinuum\Clock;
use Innmind\IO\Readable\Frame;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\{
Maybe,
Sequence,
Predicate\Instance,
};

/**
* @psalm-immutable
Expand Down Expand Up @@ -282,4 +298,125 @@ public function equals(self $method): bool
{
return $method === $this;
}

/**
* @return Frame<Sequence<Value>>
*/
public function incomingFrame(Clock $clock): Frame
{
/**
* @psalm-suppress NamedArgumentNotAllowed
* @var Frame<Sequence<Unpacked>>
*/
$frame = match ($this) {
Method::basicQosOk,
Method::basicRecoverOk,
Method::channelCloseOk,
Method::connectionCloseOk,
Method::exchangeDeclareOk,
Method::exchangeDeleteOk,
Method::queueBindOk,
Method::queueUnbindOk,
Method::transactionSelectOk,
Method::transactionCommitOk,
Method::transactionRollbackOk => Frame\NoOp::of(Sequence::of()), // no arguments
Method::basicConsumeOk => ShortString::frame()->map(Sequence::of(...)), // consumer tag
Method::basicCancelOk => ShortString::frame()->map(Sequence::of(...)), // consumer tag
Method::basicReturn => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedShortInteger::frame(), // reply code
ShortString::frame(), // reply text
ShortString::frame(), // exchange
ShortString::frame(), // routing key
),
Method::basicDeliver => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
ShortString::frame(), // consumer tag
UnsignedLongLongInteger::frame(), // delivery tag
Bits::frame(), // redelivered
ShortString::frame(), // exchange
ShortString::frame(), // routing key
),
Method::basicGetOk => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedLongLongInteger::frame(), // delivery tag
Bits::frame(), // redelivered
ShortString::frame(), // exchange
ShortString::frame(), // routing key
UnsignedLongInteger::frame(), // message count
),
Method::basicGetEmpty => ShortString::frame()->map(Sequence::of(...)), // reserved,
Method::channelOpenOk => LongString::frame()->map(Sequence::of(...)), // reserved
Method::channelFlow => Bits::frame()->map(Sequence::of(...)), // active
Method::channelFlowOk => Bits::frame()->map(Sequence::of(...)), // active
Method::channelClose => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedShortInteger::frame(), // reply code
ShortString::frame(), // reply text
UnsignedShortInteger::frame(), // failing class id
UnsignedShortInteger::frame(), // failing method id
),
Method::connectionStart => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedOctet::frame(), // major version
UnsignedOctet::frame(), // minor version
Table::frame($clock), // server properties
LongString::frame(), // mechanisms
LongString::frame(), // locales
),
Method::connectionSecure => LongString::frame()->map(Sequence::of(...)), // challenge
Method::connectionTune => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedShortInteger::frame(), // max channels
UnsignedLongInteger::frame(), // max frame size
UnsignedShortInteger::frame(), // heartbeat delay
),
Method::connectionOpenOk => ShortString::frame()->map(Sequence::of(...)), // known hosts
Method::connectionClose => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
UnsignedShortInteger::frame(), // reply code
ShortString::frame(), // reply text
UnsignedShortInteger::frame(), // failing class id
UnsignedShortInteger::frame(), // failing method id
),
Method::queueDeclareOk => Frame\Composite::of(
static fn(Unpacked ...$values) => Sequence::of(...$values),
ShortString::frame(), // queue
UnsignedLongInteger::frame(), // message count
UnsignedLongInteger::frame(), // consumer count
),
Method::queuePurgeOk => UnsignedLongInteger::frame()->map(Sequence::of(...)), // message count
Method::queueDeleteOk => UnsignedLongInteger::frame()->map(Sequence::of(...)), // message count
Method::basicAck,
Method::basicCancel,
Method::basicConsume,
Method::basicGet,
Method::basicPublish,
Method::basicQos,
Method::basicRecover,
Method::basicRecoverAsync,
Method::basicReject,
Method::channelOpen,
Method::connectionOpen,
Method::connectionSecureOk,
Method::connectionStartOk,
Method::connectionTuneOk,
Method::exchangeDeclare,
Method::exchangeDelete,
Method::queueBind,
Method::queueDeclare,
Method::queueDelete,
Method::queuePurge,
Method::queueUnbind,
Method::transactionCommit,
Method::transactionRollback,
Method::transactionSelect => throw new \LogicException('Server should never send this method'),
};

return $frame->map(
static fn($values) => $values
->keep(Instance::of(Unpacked::class))
->map(static fn($unpacked) => $unpacked->unwrap()),
);
}
}
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/Bits.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function of(bool $first, bool ...$bits): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static function of(int $value, int $scale): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/LongString.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static function of(Str $string): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static function of(Value ...$values): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(Clock $clock): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/ShortString.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static function of(Str $string): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/SignedLongInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/SignedLongLongInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/SignedOctet.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/SignedShortInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/Symbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum Symbol
case table;

/**
* @psalm-pure
*
* @return Frame<Unpacked>
*/
public static function frame(Clock $clock, string $symbol): Frame
Expand Down
4 changes: 4 additions & 0 deletions src/Transport/Frame/Value/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static function of(Map $map): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(Clock $clock): Frame
Expand Down Expand Up @@ -94,6 +96,8 @@ public function pack(): Str
}

/**
* @psalm-pure
*
* @param Unpacked<self> $unpacked
*
* @return Frame<Unpacked<self>>
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static function of(PointInTime $point): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(Clock $clock): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/UnsignedLongInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/UnsignedLongLongInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/UnsignedOctet.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static function of(int $octet): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/UnsignedShortInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static function of(int $value): self
}

/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
2 changes: 2 additions & 0 deletions src/Transport/Frame/Value/VoidValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
final class VoidValue implements Value
{
/**
* @psalm-pure
*
* @return Frame<Unpacked<self>>
*/
public static function frame(): Frame
Expand Down
4 changes: 1 addition & 3 deletions src/Transport/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ final class Protocol
{
private Clock $clock;
private Version $version;
private Reader $read;
private Connection $connection;
private Channel $channel;
private Exchange $exchange;
Expand All @@ -46,7 +45,6 @@ public function __construct(Clock $clock, ArgumentTranslator $translator)
{
$this->clock = $clock;
$this->version = Version::v091;
$this->read = new Reader($clock);
$this->connection = new Connection;
$this->channel = new Channel;
$this->exchange = new Exchange($translator);
Expand All @@ -65,7 +63,7 @@ public function version(): Version
*/
public function frame(Method $method): Frame
{
return ($this->read)($method);
return $method->incomingFrame($this->clock);
}

/**
Expand Down
Loading

0 comments on commit 3ff8c9b

Please sign in to comment.