/**
  * Decomposes binary message, which comes from the stream
  *
  * @param string $message
  *
  * @return SocketMessage
  */
 public function decomposeMessage($message)
 {
     $buffer = new Buffer($message);
     // Information about how long message is. To help it decode. Comes from the server
     // $messageLenght = (whole stream length) - (4 bytes for saved length).
     $messageLength = $buffer->readInt32LE(0);
     $messageType = new MessageType($buffer->readInt8(MessageConfiguration::MESSAGE_TYPE_OFFSET));
     $flag = $buffer->readInt8(MessageConfiguration::FLAG_OFFSET);
     $correlationID = bin2hex($buffer->read(MessageConfiguration::CORRELATION_ID_OFFSET, MessageConfiguration::CORRELATION_ID_LENGTH));
     $data = $buffer->read(MessageConfiguration::DATA_OFFSET, $messageLength - MessageConfiguration::HEADER_LENGTH);
     $communicable = $this->communicationFactory->create($messageType);
     return $communicable->handle($messageType, $correlationID, $data);
 }
 /**
  * @test
  * @dataProvider messagesToAcceptProvider
  */
 public function it_should_create_new_communication($typeOfMessage, $expectedHandler)
 {
     $messageType = $this->prophesize('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\MessageType');
     $messageType->getType()->willReturn($typeOfMessage);
     PHPUnit_Framework_Assert::assertInstanceOf($expectedHandler, $this->communicationFactory->create($messageType->reveal()));
 }
 /**
  * @test
  */
 public function it_should_create_new_communication()
 {
     $messageType = $this->prophesize('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\MessageType');
     $messageType->getType()->willReturn(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::HEARTBEAT_REQUEST);
     PHPUnit_Framework_Assert::assertInstanceOf('Madkom\\EventStore\\Client\\Domain\\Socket\\Communication\\Type\\HeartBeatRequestHandler', $this->communicationFactory->create($messageType->reveal()));
 }