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: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 
<?php

namespace SAREhub\Client\Processor;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SAREhub\Client\Message\Exchange;

/**
 * Basic router processor.
 * Routing exchanges to processor defined by routing key value returns by routingFunction.
 */
class Router implements Processor, LoggerAwareInterface
{

    /**
     * @var Processor[]
     */
    private $routes = [];

    /**
     * @var callable
     */
    private $routingFunction;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(callable $routingFunction)
    {
        $this->routingFunction = $routingFunction;
        $this->logger = new NullLogger();
    }

    public static function withRoutingFunction(callable $routingFunction): Router
    {
        return new self($routingFunction);
    }

    public function process(Exchange $exchange)
    {
        $routingKey = $this->getRoutingKeyForExchange($exchange);

        if ($route = $this->getRoute($routingKey)) {
            $this->getLogger()->debug('exchange has route', [
                'routingKey' => $routingKey,
                'exchange' => $exchange,
                'route' => $route
            ]);
            $route->process($exchange);
        } else {
            $this->getLogger()->debug('route for exchange not found', [
                'routingKey' => $routingKey,
                'exchange' => $exchange
            ]);
        }
    }

    public function getRoutingKeyForExchange(Exchange $exchange): string
    {
        $routingFunction = $this->routingFunction;
        return $routingFunction($exchange);
    }

    public function addRoute($routingKey, Processor $route): Router
    {
        $this->routes[$routingKey] = $route;
        return $this;
    }

    public function removeRoute($routingKey)
    {
        unset($this->routes[$routingKey]);
    }

    public function getRoute($routingKey): ?Processor
    {
        return $this->hasRoute($routingKey) ? $this->routes[$routingKey] : null;
    }

    public function hasRoute($routingKey): bool
    {
        return isset($this->routes[$routingKey]);
    }

    /**
     * @return Processor[]
     */
    public function getRoutes(): array
    {
        return $this->routes;
    }

    public function getRoutingFunction(): callable
    {
        return $this->routingFunction;
    }

    public function getLogger()
    {
        return $this->logger;
    }

    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function __toString()
    {
        $routes = [];
        foreach ($this->getRoutes() as $key => $route) {
            $routes[] = $key . ' => ' . $route;
        }

        return 'Router[ {' . implode('}, {', $routes) . '}]';
    }
}