Completed
Push — master ( a71924...e2ef70 )
by Ryosuke
02:54
created

ConnectionCounter::removeDestination()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 1
crap 4
1
<?php
2
3
namespace mpyw\Co\Internal;
4
use mpyw\Co\Co;
5
use mpyw\Co\Internal\CoOption;
6
use mpyw\Co\CURLException;
7
use mpyw\RuntimePromise\Deferred;
8
9
class ConnectionCounter
10
{
11
    /**
12
     * Options.
13
     * @var CoOption
14
     */
15
    private $options;
16
17
    /**
18
     * The number of whole running TCP connections.
19
     * @var array
20
     */
21
    private $connectionCount = 0;
22
23
    /**
24
     * Counts per destination.
25
     *   key   => identifier
26
     *   value => count
27
     * @var array
28
     */
29
    private $destinations = [];
30
31
    /**
32
     * Constructor.
33
     * @param CoOption $options
34
     */
35 26
    public function __construct(CoOption $options)
36 26
    {
37 26
        $this->options = $options;
38 26
    }
39
40
    /**
41
     * Add destination info.
42
     * @param resource $ch
43
     */
44 13
    public function addDestination($ch)
45 13
    {
46 13
        $id = $this->getIdentifier($ch);
47 13
        if ($id === '') {
48 12
            ++$this->connectionCount;
49 12
            return;
50
        }
51 1
        if (empty($this->destinations[$id])) {
52 1
            $this->destinations[$id] = 1;
53 1
            ++$this->connectionCount;
54 1
            return;
55
        }
56 1
        ++$this->destinations[$id];
57 1
    }
58
59
    /**
60
     * Check if internal cURL pool is filled.
61
     * @param resource $ch
62
     * @return bool
63
     */
64 13
    public function isPoolFilled($ch)
65 13
    {
66 13
        $id = $this->getIdentifier($ch);
67 13
        if ($id !== '' && !empty($this->destinations[$id])) {
68 1
            return false;
69
        }
70 13
        return $this->options['concurrency'] > 0
71 13
            && $this->connectionCount >= $this->options['concurrency'];
72
    }
73
74
    /**
75
     * Remove destination info.
76
     * @param resource $ch
77
     */
78 10
    public function removeDestination($ch)
79 10
    {
80 10
        $id = $this->getIdentifier($ch);
81 10
        if ($id === '') {
82 9
            --$this->connectionCount;
83 9
            return;
84
        }
85 1
        if (empty($this->destinations[$id]) || $this->destinations[$id] === 1) {
86 1
            unset($this->destinations[$id]);
87 1
            --$this->connectionCount;
88 1
            return;
89
        }
90 1
        --$this->destinations[$id];
91 1
    }
92
93
    /**
94
     * Push into queue.
95
     * @param resource $ch
96
     * @return string
97
     */
98 13
    private function getIdentifier($ch)
99 13
    {
100 13
        return $this->options['group'] ? (string)curl_getinfo($ch, CURLINFO_PRIVATE) : '';
101
    }
102
}
103