1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 
<?php

namespace SAREhub\Commons\Zmq\PublishSubscribe;

use SAREhub\Commons\Zmq\ZmqSocketSupport;

/**
 * Represents publisher ZMQ socket
 */
class Publisher extends ZmqSocketSupport
{

    public function __construct(\ZMQSocket $socket)
    {
        parent::__construct($socket);
    }

    /**
     * @param \ZMQContext $context
     * @return Publisher
     */
    public static function inContext(\ZMQContext $context)
    {
        return new self($context->getSocket(\ZMQ::SOCKET_PUB, null, null));
    }

    /**
     * @param string $topic
     * @param string $message
     * @param bool $wait
     */
    public function publish($topic, $message, $wait = false)
    {
        if (!$this->isBindedOrConnected()) {
            throw new \LogicException("Publishing message when not binded or connected");
        }

        $mode = ($wait) ? 0 : \ZMQ::MODE_DONTWAIT;
        $this->getSocket()->sendmulti([$topic, $message], $mode);
    }
}