PollableChannel::receive()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 12
Ratio 54.55 %

Importance

Changes 6
Bugs 5 Features 0
Metric Value
c 6
b 5
f 0
dl 12
loc 22
rs 8.6737
cc 6
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace PEIP\Channel;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * PollableChannel
15
 * Basic concete implementation of a pollable channel.
16
 *
17
 * @author Timo Michna <timomichna/yahoo.de>
18
 * @extends \PEIP\ABS\Channel\Channel
19
 * @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\PollableChannel
20
 */
21
class PollableChannel extends \PEIP\ABS\Channel\Channel implements \PEIP\INF\Channel\PollableChannel
22
{
23
    const
24
        EVENT_PRE_RECEIVE = 'pre_receive',
25
        EVENT_POST_RECEIVE = 'post_receive',
26
        HEADER_MESSAGE = 'MESSAGE';
27
28
    protected $messages = [];
29
30
    /**
31
     * Sends a message on the channel.
32
     *
33
     * @param \PEIP\INF\Message\Message $message the message to send
34
     *
35
     * @return
36
     */
37
    protected function doSend(\PEIP\INF\Message\Message $message)
38
    {
39
        $this->messages[] = $message;
40
41
        return true;
42
    }
43
44
    /**
45
     * Receives a message from the channel.
46
     *
47
     * @event preReceive
48
     * @event postReceive
49
     *
50
     * @param int $timeout timout for receiving a message
51
     *
52
     * @return
53
     */
54
    public function receive($timeout = 0)
55
    {
56
        $this->doFireEvent(self::EVENT_PRE_RECEIVE);
57
        $message = null;
58 View Code Duplication
        if ($timeout == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $message = $this->getMessage();
60
        } elseif ($timeout < 0) {
61
            while (!$message = $this->getMessage()) {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
62
            }
63
        } else {
64
            $time = time() + $timeout;
65
            while (($time > time()) && !$message = $this->getMessage()) {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
66
            }
67
        }
68
        $this->doFireEvent(
69
            self::EVENT_PRE_RECEIVE, [
70
                self::HEADER_MESSAGE => $message,
71
            ]
72
        );
73
74
        return $message;
75
    }
76
77
    /**
78
     * Returns a message from top of the message stack.
79
     *
80
     * @return \PEIP\INF\Message\Message message from top of the message stack
81
     */
82
    protected function getMessage()
83
    {
84
        return array_shift($this->messages);
85
    }
86
87
    /**
88
     * Deletes all messages on the message stack.
89
     *
90
     * @return
91
     */
92
    public function clear()
93
    {
94
        $this->messages = [];
95
    }
96
97
    /**
98
     * Removes all messages not accepted by a given message-selector from the message-stack.
99
     *
100
     * @param \PEIP\INF\Message\Message_Selector $selector the selector to accept messages
101
     *
102
     * @return array accepted messages
103
     */
104 View Code Duplication
    public function purge(\PEIP\INF\Selector\MessageSelector $selector)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        foreach ($this->messages as $key => $message) {
107
            if (!$selector->acceptMessage($message)) {
108
                unset($this->messages[$key]);
109
            }
110
        }
111
112
        return $this->messages;
113
    }
114
}
115