<?php
namespace App\models\Logistic\Api\Custom\Client;
use App\models\Logistic\Api\Custom\AbstractClient;
use App\models\Logistic\Api\Custom\ClientConfig;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class Mq extends AbstractClient{
    protected $xml;
    protected $client;
    protected $config;
    protected $chanel;
    protected $clientConfig;
    public function __construct(ClientConfig $config){
        $this->clientConfig=$config;
    }
    public function get(){

    }
    public function send()
    {
        $queuename = $this->config['queue'];
        $channel = $this->connection->channel();
        $channel->queue_declare($queuename, true, true, false);
        $msg = new AMQPMessage($this->xml);
        $v = $channel->basic_publish($msg, '', $queuename);
        $channel->close();
        return $v;
    }
    protected function getClient(){
        if(!$this->client){
            $this->config =  $this->clientConfig->getConfig();
            $con = new AMQPStreamConnection(
                $this->config['host'],
                $this->config['port'],
                $this->config['username'],
                $this->config['password'],
                $this->config['vhost'],
                false,
                'AMQPLAIN',
                'null',
                'en_US'
            );
            if(!$con->isConnected()) {
                throw new \Exception('连接失败1');
            }
            $this->client = $con;
            $this->chanel = $this->connection->channel();
        }
    }
    public function setSendInf(...$sendInf)
    {
        $xml = $sendInf[0];
        $this->xml = $xml;
        return $this;
    }
}
