<?php

namespace xmvc;

use xmvc\libQueue\Beanstalk;
use xmvc\libQueue\RabbitMQ;
use xmvc\interface\IQueue;

/**
* 队列
*/
class Queue
{
    /**
     * 创建一个队列
     *
     * @param string $type beanstalk 或 rabbitMQ
     * @param string $dns 配置项
     * @return IQueue
     */
    public static function create(string $type = 'beanstalk', string $dns = ''): IQueue
    {
        $type = strtolower($type);
        $types = ['beanstalk', 'rabbitMQ'];
        if (!in_array($type, $types)) {
            error(lg('未知的队列类型  {:1} 只支持 {:2}', $type, implode(',', $types)));
        }
        if ($type == 'beanstalk') {
            $options = array_merge([
                'host' => '127.0.0.1',
                'port' => 11300,
                'tubeName' => 'default',
                'persist' => false,
                'multiTasking' => false,
                'dbCfg' => 'xmvc',
                'table' => 'beanstalkd',
            ], $dns ? cfg($dns) : []);
            $object = new Beanstalk($options);
        } elseif ($type == 'rabbitMQ') {
            $options = array_merge([
                'host' => '127.0.0.1',
                'port' => 0,
            ], $dns ? cfg($dns) : []);
            $object = new RabbitMQ($options);
        }
        return $object;
    }
}
