Completed
Push — master ( 0b9fa7...ac04d3 )
by Ryosuke
03:05
created

ManualScheduler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 3
dl 14
loc 91
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 6 2
A isEmpty() 0 4 2
A addImmediate() 14 14 4
A addReserved() 0 5 2
A interruptConsume() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace mpyw\Co\Internal;
4
use mpyw\Co\CURLException;
5
use mpyw\RuntimePromise\Deferred;
6
7
class ManualScheduler extends AbstractScheduler
8
{
9
    /**
10
     * cURL handles those have not been dispatched.
11
     * @var array
12
     */
13
    private $queue = [];
14
15
    /**
16
     * TCP connection counter.
17
     * @var ConnectionCounter
18
     */
19
    private $counter;
20
21
    /**
22
     * Constructor.
23
     * Initialize cURL multi handle.
24
     * @param CoOption $options
25
     * @param resource $mh      curl_multi
26
     */
27 30
    public function __construct(CoOption $options, $mh)
28 30
    {
29 30
        $this->mh = $mh;
30 30
        $this->options = $options;
31 30
        $this->counter = new ConnectionCounter($options);
32 30
    }
33
34
    /**
35
     * Call curl_multi_add_handle() or push into queue.
36
     * @param resource $ch
37
     * @param Deferred $deferred
38
     */
39 10
    public function add($ch, Deferred $deferred = null)
40 10
    {
41 10
        $this->counter->isPoolFilled($ch)
42 2
            ? $this->addReserved($ch, $deferred)
43 10
            : $this->addImmediate($ch, $deferred);
44 10
    }
45
46
    /**
47
     * Are there no cURL handles?
48
     * @return bool
49
     */
50 21
    public function isEmpty()
51 21
    {
52 21
        return !$this->added && !$this->queue;
53
    }
54
55
    /**
56
     * Call curl_multi_add_handle().
57
     * @param resource $ch
58
     * @param Deferred $deferred
59
     */
60 10 View Code Duplication
    private function addImmediate($ch, Deferred $deferred = null)
1 ignored issue
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...
61 10
    {
62 10
        $errno = curl_multi_add_handle($this->mh, $ch);
63 10
        if ($errno !== CURLM_OK) {
64
            // @codeCoverageIgnoreStart
65
            $msg = curl_multi_strerror($errno) . ": $ch";
66
            $deferred && $deferred->reject(new \RuntimeException($msg));
67
            return;
68
            // @codeCoverageIgnoreEnd
69
        }
70 10
        $this->added[(string)$ch] = $ch;
71 10
        $this->counter->addDestination($ch);
72 10
        $deferred && $this->deferreds[(string)$ch] = $deferred;
73 10
    }
74
75
    /**
76
     * Push into queue.
77
     * @param resource $ch
78
     * @param Deferred $deferred
79
     */
80 2
    private function addReserved($ch, Deferred $deferred = null)
81 2
    {
82 2
        $this->queue[(string)$ch] = $ch;
83 2
        $deferred && $this->deferreds[(string)$ch] = $deferred;
84 2
    }
85
86
    /**
87
     * Add cURL handles from waiting queue.
88
     * @param array $entry
89
     */
90 10
    protected function interruptConsume(array $entry)
91 10
    {
92 10
        $this->counter->removeDestination($entry['handle']);
93 10
        if ($this->queue) {
94 2
            $this->add(array_shift($this->queue));
95
        }
96 10
    }
97
}
98