Router::resolveChannel()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace PEIP\ABS\Router;
4
5
namespace PEIP\ABS\Router;
6
7
/*
8
 * This file is part of the PEIP package.
9
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
/*
16
 * PEIP\ABS\Router\Router
17
 * Basic abstract implementation of a message router.
18
 *
19
 * @author Timo Michna <timomichna/yahoo.de>
20
 * @package PEIP
21
 * @subpackage router
22
 * @extends \PEIP\Pipe\Pipe
23
 * @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Channel\Channel, \PEIP\INF\Handler\Handler, \PEIP\INF\Message\MessageBuilder
24
 */
25
26
27
28
abstract class Router extends \PEIP\Pipe\Pipe
29
{
30
    const
31
        EVENT_PRE_RESOLVE = 'pre_resolve',
32
        EVENT_POST_RESOLVE = 'post_resolve',
33
        EVENT_ERR_RESOLVE = 'err_resolve',
34
        HEADER_CHANNEL = 'CHANNEL',
35
        HEADER_CHANNEL_NAME = 'CHANNEL_NAME',
36
        HEADER_CHANNEL_RESOLVER = 'HEADER_CHANNEL_RESOLVER';
37
38
    protected $channelResolver;
39
40
    /**
41
     * constructor.
42
     *
43
     * @param \PEIP\INF\Channel\Channel_Resolver $channelResolver channel resolver for the router
44
     * @param \PEIP\INF\Channel\Channel          $inputChannel    the input channel for the router
45
     *
46
     * @return
47
     */
48
    public function __construct(\PEIP\INF\Channel\Channel_Resolver $channelResolver, \PEIP\INF\Channel\Channel $inputChannel)
49
    {
50
        $this->channelResolver = $channelResolver;
51
        $this->setInputChannel($inputChannel);
52
    }
53
54
    /**
55
     * Sets the channel resolver for the router.
56
     *
57
     * @param \PEIP\INF\Channel\Channel_Resolver $channelResolver the channel resolver for the router
58
     *
59
     * @return
60
     */
61
    public function setChannelResolver(\PEIP\INF\Channel\Channel_Resolver $channelResolver)
62
    {
63
        $this->doFireEvent(self::EVENT_CHANNEL_RESOLVER_SET, [self::HEADER_CHANNEL_RESOLVER => $channelResolver]);
64
        $this->channelResolver = $channelResolver;
65
        $this->doFireEvent(self::EVENT_CHANNEL_RESOLVER_SET, [self::HEADER_CHANNEL_RESOLVER => $channelResolver]);
66
    }
67
68
    /**
69
     * Sends given message on all accepted reply-channels.
70
     *
71
     * @param \PEIP\INF\Message\Message $message the message to reply with
72
     *
73
     * @return
74
     */
75
    protected function doReply(\PEIP\INF\Message\Message $message)
76
    {
77
        $channels = (array) $this->selectChannels($message);
78
        foreach ($channels as $channel) {
79
            $this->setOutputChannel($this->resolveChannel($channel));
80
            $this->replyMessage($message);
81
        }
82
    }
83
84
    /**
85
     * Resolves a channel from name or channel-instance.
86
     * Returns first argument directly if it is intance of \PEIP\INF\Channel\Channel.
87
     * Else resolves channel through registered channel-resolver.
88
     *
89
     * @param mixed $channel channel-name or instance of \PEIP\INF\Channel\Channel
90
     *
91
     * @return
92
     */
93
    protected function resolveChannel($channel)
94
    {
95
        $this->doFireEvent(self::EVENT_PRE_RESOLVE, [self::HEADER_CHANNEL => $channel]);
96
        if (!($channel instanceof \PEIP\INF\Channel\Channel)) {
97
            $channelName = $channel;
98
            $channel = $this->channelResolver->resolveChannelName($channelName);
99
            if (!$channel) {
100
                $this->doFireEvent(self::EVENT_ERR_RESOLVE, [self::HEADER_CHANNEL_NAME => $channelName]);
101
                throw new \RuntimeException('Could not resolve Channel : '.$channelName);
102
            }
103
        }
104
        $this->doFireEvent(self::EVENT_POST_RESOLVE, [self::HEADER_CHANNEL => $channel]);
105
106
        return $channel;
107
    }
108
109
    /**
110
     * Selects channel(s) to route given message to.
111
     *
112
     * @abstract
113
     *
114
     * @param \PEIP\INF\Message\Message $message message to route
115
     */
116
    abstract protected function selectChannels(\PEIP\INF\Message\Message $message);
117
}
118