Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function attach(MessageHandler $handler)
 {
     $this->server->on('request', function (Request $request, Response $response) use($handler) {
         $data = (object) array('message' => '');
         $request->on('data', function ($chunk) use($data) {
             $data->message .= $chunk;
         });
         $request->on('end', function () use($data, $handler, $response) {
             try {
                 $handler->receive($data->message);
                 $ack = new JobAcknowledgement();
                 $ack->setCreatedOn(new \DateTime());
             } catch (RuntimeException $e) {
                 $ack = new JobNotAcknowledgement();
                 $ack->setCreatedOn(new \DateTime());
                 $ack->setReason($e->getMessage());
             }
             $json = $ack->toJson();
             $response->writeHead(200, array('Content-type' => 'application/json; charset=utf-8', 'Content-length' => strlen($json)));
             $response->write($json);
             $response->end();
         });
         $request->on('error', function ($error) use($handler) {
             $handler->error($error);
         });
     });
 }
Esempio n. 2
0
 public function getData()
 {
     $ack = new JobAcknowledgement();
     $nack = new JobNotAcknowledgement();
     $nack->setReason('Man machine');
     return array(array($ack, $ack->toJson()), array($nack, $nack->toJson()));
 }
 /** @test */
 public function itMustBeSerializable()
 {
     $ack = new JobNotAcknowledgement();
     $date = new \DateTime('+5days');
     $ack->setCreatedOn($date);
     $ack->setReason('Man machine');
     $data = json_decode($ack->toJson(), true);
     $this->assertEquals($date->format(DATE_ATOM), $data['createdOn']);
     $this->assertEquals('Man machine', $data['reason']);
     $unserialized = MessageFactory::fromJson($ack->toJson());
     $this->assertEquals($ack->getCreatedOn()->format(DATE_ATOM), $unserialized->getCreatedOn()->format(DATE_ATOM));
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function attach(MessageHandler $handler)
 {
     $pull = $this->pull;
     $this->pull->on('error', function ($error) use($handler) {
         $handler->error($error);
     });
     $this->pull->on('message', function ($message) use($pull, $handler) {
         try {
             $handler->receive($message);
             $ack = new JobAcknowledgement();
             $ack->setCreatedOn(new \DateTime());
         } catch (RuntimeException $e) {
             $ack = new JobNotAcknowledgement();
             $ack->setCreatedOn(new \DateTime());
             $ack->setReason($e->getMessage());
         }
         $pull->send($ack->toJson());
     });
 }