TransferQueued::segments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Transfer queued
4
 * User: moyo
5
 * Date: 21/11/2017
6
 * Time: 4:24 PM
7
 */
8
9
namespace Carno\Traced\Chips;
10
11
use Closure;
12
13
trait TransferQueued
14
{
15
    /**
16
     * @var array
17
     */
18
    private $buffer = [];
19
20
    /**
21
     * @var int
22
     */
23
    private $packed = 1;
24
25
    /**
26
     * @var int
27
     */
28
    private $overflow = 0;
29
30
    /**
31
     * @param int $packed
32
     * @param int $overflow
33
     */
34
    private function options(int $packed = 200, int $overflow = 20000) : void
35
    {
36
        $this->packed = $packed;
37
        $this->overflow = $overflow;
38
    }
39
40
    /**
41
     * @param string $data
42
     */
43
    private function stashing(string $data) : void
44
    {
45
        count($this->buffer) < $this->overflow && $this->buffer[] = $data;
46
    }
47
48
    /**
49
     * @return int
50
     */
51
    private function stashed() : int
52
    {
53
        return count($this->buffer);
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    private function segments() : array
60
    {
61
        return array_splice($this->buffer, 0, $this->packed);
62
    }
63
64
    /**
65
     * @param Closure $program
66
     */
67
    private function spouting(Closure $program)
68
    {
69
        while ($this->stashed()) {
70
            $program($this->segments());
71
        }
72
    }
73
}
74