Uecode\Bundle\QPushBundle\Provider\FileProvider::receive PHP Method

receive() public method

public receive ( array $options = [] ) : Uecode\Bundle\QPushBundle\Message\Message[]
$options array
return Uecode\Bundle\QPushBundle\Message\Message[]
    public function receive(array $options = [])
    {
        $finder = new Finder();
        $finder->files()->ignoreDotFiles(true)->ignoreUnreadableDirs(true)->ignoreVCS(true)->name('*.json')->in($this->queuePath);
        if ($this->options['message_delay'] > 0) {
            $finder->date(sprintf('< %d seconds ago', $this->options['message_delay']));
        }
        $finder->date(sprintf('> %d seconds ago', $this->options['message_expiration']));
        $messages = [];
        /** @var SplFileInfo $file */
        foreach ($finder as $file) {
            $filePointer = fopen($file->getRealPath(), 'r+');
            $id = substr($file->getFilename(), 0, -5);
            if (!isset($this->filePointerList[$id]) && flock($filePointer, LOCK_EX | LOCK_NB)) {
                $this->filePointerList[$id] = $filePointer;
                $messages[] = new Message($id, json_decode($file->getContents(), true), []);
            } else {
                fclose($filePointer);
            }
            if (count($messages) === (int) $this->options['messages_to_receive']) {
                break;
            }
        }
        return $messages;
    }

Usage Example

Exemplo n.º 1
0
 public function testOnMessageReceived()
 {
     $this->provider->create();
     $id = $this->provider->publish(['foo' => 'bar']);
     $path = substr(hash('md5', $id), 0, 3);
     $this->assertTrue(is_file($this->basePath . DIRECTORY_SEPARATOR . $this->queueHash . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $id . '.json'));
     $this->provider->onMessageReceived(new MessageEvent('test', $this->provider->receive()[0]));
     $this->assertFalse(is_file($this->basePath . DIRECTORY_SEPARATOR . $this->queueHash . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $id . '.json'));
 }