/**
  * @inheritdoc
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $channelName = $package->channel;
     if (!$this->channels->exists($channelName)) {
         $chn = new Channel($channelName);
         $this->channels->add($chn);
     }
     $this->channels->get($channelName)->addConnection($from);
 }
 public function testSendsToConnectionsInChannel()
 {
     $chn = new Channel('my-channel');
     $chn->addConnection($this->connection);
     $this->channels->add($chn);
     $package = (object) ['from' => 'Helmut', 'channel' => 'my-channel', 'message' => 'Hello World'];
     $this->command->run($this->connection, $package);
     $res = json_decode($this->packages[0]);
     $this->assertEquals('Hello World', $res->message);
 }
 /**
  * @inheritdoc
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $channelName = $package->channel;
     $chn = $this->channels->get($channelName);
     if (!$chn) {
         return;
     }
     $chn->removeConnection($from);
     // remove the channel if it is empty.
     if (count($chn->getConnections()) === 0) {
         $this->channels->remove($channelName);
     }
 }
 public function setUp()
 {
     parent::setUp();
     $this->channels = new ChannelCollection();
     $this->channels->add(new Channel('my-channel'));
     $this->channels->add(new Channel('my-other-channel'));
     $this->command = new ListChannelsCommand($this->channels);
     $this->packages = [];
     $parent = $this;
     $mock = $this->prophesize(ConnectionInterface::class);
     $mock->close()->willReturn(null);
     $mock->send(Argument::any())->will(function ($args) use($parent) {
         $parent->packages[] = $args[0];
     });
     $this->connection = $mock->reveal();
 }
 /**
  * @inheritdoc
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $channelName = $package->channel;
     $fromName = $package->from;
     $message = $package->message;
     // can't send to non-existent channels
     if (!$this->channels->exists($channelName)) {
         return;
     }
     $chn = $this->channels->get($channelName);
     // can't send to channels you're not apart of
     if (!$chn->connectionExists($from)) {
         return;
     }
     // this json-encoded string will be sent over the socket
     $outgoing = json_encode(['command' => $this->getName(), 'channel' => $channelName, 'message' => $message, 'from' => $fromName, 'timestamp' => time()]);
     foreach ($chn->getConnections() as $conn) {
         $conn->send($outgoing);
     }
 }
 /**
  * @param ConnectionInterface $from
  * @param \stdClass $package
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $from->send(json_encode(['command' => $this->getName(), 'channels' => $this->channels->getChannelNames()]));
 }
 public function testChannelIsRemovedWhenEmpty()
 {
     $package = (object) ['channel' => 'my-channel'];
     $this->command->run($this->connection, $package);
     $this->assertFalse($this->channels->exists('my-channel'));
 }
 public function testJoinsChannelCorrectly()
 {
     $package = (object) ['channel' => 'my-channel'];
     $this->command->run($this->connection, $package);
     $this->assertTrue($this->channels->get('my-channel')->connectionExists($this->connection));
 }
 public function testGetChannelNames()
 {
     $chn = new Channel('one');
     $this->channels->add($chn);
     $this->assertEquals(['one'], $this->channels->getChannelNames());
 }