MessageHandler::doHandle()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 1
dl 0
loc 1
1
<?php
2
3
namespace PEIP\ABS\Handler;
4
5
namespace PEIP\ABS\Handler;
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\Handler\MessageHandler
17
 * Base class for all message handling classes.
18
 *
19
 * @author Timo Michna <timomichna/yahoo.de>
20
 * @package PEIP
21
 * @subpackage handler
22
 * @implements \PEIP\INF\Handler\Handler
23
 */
24
25
use PEIP\Pipe\Pipe;
26
27
abstract class MessageHandler extends \PEIP\ABS\Base\Connectable implements \PEIP\INF\Handler\Handler
28
{
29
    protected $inputChannel,
30
        $unwrapEvents = false;
31
32
    /**
33
     * Handles a message. Delegates the handling of the message to
34
     * abstract 'doHandle' method which must be implemented by extending classes.
35
     *
36
     * @see PEIP\ABS\Handler\MessageHandler::doHandle
37
     * @implements \PEIP\INF\Handler\Handler
38
     *
39
     * @param object $message the message to handle
40
     *
41
     * @return
42
     */
43
    public function handle($message)
44
    {
45
        $this->doHandle($this->getMessageFromObject($message));
46
    }
47
48
    /**
49
     * Sets the input channel for the message handler.
50
     * Delegates connecting of input-channel to protected method 'doSetInputChannel',
51
     * which can be overwritten by extending classes.
52
     *
53
     * @see PEIP\ABS\Handler\MessageHandler::doSetInputChannel
54
     *
55
     * @param \PEIP\INF\Channel\Channel $inputChannel the input-channel
56
     *
57
     * @return MessageHandler $this;
58
     */
59
    public function setInputChannel(\PEIP\INF\Channel\Channel $inputChannel)
60
    {
61
        $this->doSetInputChannel($inputChannel);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Connects the handler to the input channel.
68
     * When input-channel is instance of \PEIP\INF\Channel\SubscribableChannel,
69
     * the handler subscribes to the channel.
70
     * When input-channel is instance of \PEIP\INF\Channel\PollableChannel, the
71
     * handler listens to the 'preSend' event of the channel and receives
72
     * a message, when the event occures.
73
     *
74
     * @param \PEIP\INF\Channel\Channel $inputChannel the input-channel to connect the handler to
75
     *
76
     * @return
77
     */
78
    protected function doSetInputChannel(\PEIP\INF\Channel\Channel $inputChannel)
79
    {
80
        $this->inputChannel = $inputChannel;
81
        if ($this->inputChannel instanceof \PEIP\INF\Channel\SubscribableChannel) {
82
            $this->inputChannel->subscribe($this);
83
        } else {
84
            $this->unwrapEvents = true;
85
            $this->inputChannel->connect('postSend', $this);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PEIP\INF\Channel\Channel as the method connect() does only exist in the following implementations of said interface: DrinkAggregator, DrinkRouter, OrderSplitter, PEIP\ABS\Channel\Channel, PEIP\ABS\Channel\PollableChannel, PEIP\ABS\Channel\SubscribableChannel, PEIP\ABS\Pipe\EventPipe, PEIP\ABS\Router\Router, PEIP\ABS\Service\ServiceActivator, PEIP\ABS\Splitter\MessageSplitter, PEIP\ABS\Transformer\ContentTransformer, PEIP\ABS\Transformer\Transformer, PEIP\Channel\DirectChannel, PEIP\Channel\PollableChannel, PEIP\Channel\PriorityChannel, PEIP\Channel\PublishSubscribeChannel, PEIP\Channel\QueueChannel, PEIP\Listener\Wiretap, PEIP\Pipe\CommandPipe, PEIP\Pipe\EventPipe, PEIP\Pipe\FixedEventPipe, PEIP\Pipe\Pipe, PEIP\Pipe\SimpleEventPipe, PEIP\Service\HeaderServiceActivator, PEIP\Service\ServiceActivator, PEIP\Service\SplittingServiceActivator, PEIP\Service\StringServiceActivator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
        }
87
    }
88
89
    protected function getMessageFromObject($object)
90
    {
91
        $content = $object->getContent();
92
        if ($this->unwrapEvents
93
            && $object instanceof \PEIP\INF\Event\Event
94
            && $object->getName() == 'postSend'
95
            && $object->hasHeader(Pipe::HEADER_MESSAGE)
96
            && $content instanceof \PEIP\INF\Channel\PollableChannel
97
            ) {
98
            $object = $content->receive();
99
        }
100
        if (!($object instanceof \PEIP\INF\Message\Message)) {
101
            throw new \Exception('Could not retrieve Message from Message-Argument'.print_r($object, 1));
102
        }
103
104
        return $object;
105
    }
106
107
    /**
108
     * Returns the input-channel for this handler.
109
     *
110
     * @return \PEIP\INF\Channel\Channel input-channel for this handler
111
     */
112
    public function getInputChannel()
113
    {
114
        return $this->inputChannel;
115
    }
116
117
    /**
118
     * Does the message handling logic for the handler.
119
     * Must be implemented by extending classes.
120
     *
121
     * @abstract
122
     *
123
     * @param \PEIP\INF\Message\Message $message the message to handle
124
     */
125
    abstract protected function doHandle(\PEIP\INF\Message\Message $message);
126
}
127