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: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 
<?php

namespace SAREhub\Commons\Zmq\PublishSubscribe;

use SAREhub\Commons\Zmq\ZmqSocketSupport;

/**
 * Represents subscriber ZMQ socket
 */
class Subscriber extends ZmqSocketSupport
{

    /**
     * @var array
     */
    private $topics = [];
    private $subscribeAll = false;

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

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

    /**
     * @param bool $wait
     * @return null|array
     */
    public function receive($wait = false)
    {
        if ($this->isBindedOrConnected()) {
            $mode = ($wait) ? 0 : \ZMQ::MODE_DONTWAIT;
            if ($parts = $this->getSocket()->recvMulti($mode)) {
                $message = ['topic' => $parts[0], 'body' => $parts[1]];
                if ($this->isSubscribed($message['topic'])) {
                    return $message;
                }
            }

            return null;
        }

        throw new \LogicException("Can't receive message when socket isn't connected or binded");
    }

    public function subscribeAll()
    {
        $this->subscribeAll = true;
    }

    /**
     * @param string $topic
     * @return $this
     */
    public function subscribe($topic)
    {
        if (!$this->isSubscribed($topic)) {
            $this->getSocket()->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, $topic);
            $this->topics[$topic] = true;
        }

        return $this;
    }

    public function unsubscribe($topic)
    {
        if ($this->isSubscribed($topic)) {
            $this->getSocket()->setSockOpt(\ZMQ::SOCKOPT_UNSUBSCRIBE, $topic);
            unset($this->topics[$topic]);
        }

        return $this;
    }

    /**
     * @param string $topic
     * @return bool
     */
    public function isSubscribed($topic)
    {
        return $this->subscribeAll || isset($this->topics[$topic]);
    }

    /**
     * @return array
     */
    public function getTopics()
    {
        return array_keys($this->topics);
    }
}