PollableChannel::receive()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 12
Ratio 75 %

Importance

Changes 6
Bugs 5 Features 0
Metric Value
c 6
b 5
f 0
dl 12
loc 16
rs 8.8571
cc 6
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace PEIP\ABS\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
     * PEIP\ABS\Channel\PollableChannel.
15
     *
16
     * @author Timo Michna <timomichna/yahoo.de>
17
     * @extends \PEIP\ABS\Channel\Channel
18
     * @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\PollableChannel
19
     */
20
    class PollableChannel extends \PEIP\ABS\Channel\Channel implements \PEIP\INF\Channel\PollableChannel
21
    {
22
        protected $messages = [];
23
24
    /**
25
     * @param $message
26
     *
27
     * @return
28
     */
29
    protected function doSend(\PEIP\INF\Message\Message $message)
30
    {
31
        $this->messages[] = $message;
32
    }
33
34
    /**
35
     * @param $timeout
36
     *
37
     * @return
38
     */
39
    public function receive($timeout = -1)
40
    {
41
        $message = null;
42 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...
43
            $message = $this->getMessage();
44
        } elseif ($timeout < 0) {
45
            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...
46
            }
47
        } else {
48
            $time = time() + $timeout;
49
            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...
50
            }
51
        }
52
53
        return $message;
54
    }
55
56
    /**
57
     * @return
58
     */
59
    protected function getMessage()
60
    {
61
        return array_shift($this->messages);
62
    }
63
64
    /**
65
     * @return
66
     */
67
    public function clear()
68
    {
69
        $this->messages = [];
70
    }
71
72
    /**
73
     * @param $selector
74
     *
75
     * @return
76
     */
77 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...
78
    {
79
        foreach ($this->messages as $key => $message) {
80
            if (!$selector->acceptMessage($message)) {
81
                unset($this->messages[$key]);
82
            }
83
        }
84
85
        return $this->messages;
86
    }
87
    }
88