Passed
Push — master ( 8a631f...a79a09 )
by Zoilo
01:53
created

MemorySpanPool::findFinishedAndDelete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9666
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Pool\Memory;
4
5
use ZoiloMora\ElasticAPM\Events\Span\Span;
6
use ZoiloMora\ElasticAPM\Events\Transaction\Transaction;
7
use ZoiloMora\ElasticAPM\Pool\SpanPool;
8
9
final class MemorySpanPool extends MemoryPool implements SpanPool
10
{
11
    /**
12
     * @param Span $item
13
     *
14
     * @return void
15
     */
16
    public function put(Span $item)
17
    {
18
        $this->items[] = $item;
19
    }
20
21
    /**
22
     * @param Transaction $transaction
23
     *
24
     * @return Span[]
25
     */
26
    public function findFinishedAndDelete(Transaction $transaction)
27
    {
28
        $result = [];
29
30
        /** @var Span $item */
31
        foreach ($this->items as $key => $item) {
32
            if ($item->transactionId() !== $transaction->id()) {
33
                continue;
34
            }
35
36
            if (false === $item->isFinished()) {
37
                continue;
38
            }
39
40
            $result[] = $item;
41
            unset($this->items[$key]);
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * @return Span|null
49
     */
50
    public function findLastUnfinished()
51
    {
52
        $items = array_reverse($this->items);
53
54
        /** @var Span $item */
55
        foreach ($items as $item) {
56
            if (true === $item->isFinished()) {
57
                continue;
58
            }
59
60
            return $item;
61
        }
62
63
        return null;
64
    }
65
}
66