<?php declare(strict_types=1);

/**
 * This file is part of PMG\Queue
 *
 * Copyright (c) PMG <https://www.pmg.com>
 *
 * For full copyright information see the LICENSE file distributed
 * with this source code.
 *
 * @license     http://opensource.org/licenses/Apache-2.0 Apache-2.0
 */

namespace PMG\Queue;

/**
 * The default implementation of the producer. Uses a router to look up
 * where the message should go then adds it to the queue.
 *
 * @since   2015-06-09
 */
final class DefaultProducer implements Producer
{
    use MessageNames;

    /**
     * @var Driver
     */
    private $driver;

    /**
     * @var Router
     */
    private $router;

    public function __construct(Driver $driver, Router $router)
    {
        $this->driver = $driver;
        $this->router = $router;
    }

    /**
     * {@inheritdoc}
     */
    public function send(object $messageOrEnvelope) : void
    {
        $message = $messageOrEnvelope instanceof Envelope ? $messageOrEnvelope->unwrap() : $messageOrEnvelope;

        $queueName = $this->router->queueFor($message);
        if (!$queueName) {
            throw new Exception\QueueNotFound(sprintf(
                'Could not find a queue for "%s"',
                self::nameOf($message)
            ));
        }

        $this->driver->enqueue($queueName, $messageOrEnvelope);
    }
}
