Completed
Pull Request — master (#41)
by Nicolas
07:53
created

MultipleTracker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 51
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasBeenStarted() 0 6 1
A getTracker() 0 10 2
A start() 0 6 1
A markAsReceived() 0 6 1
A isAllHasBeenReceived() 0 6 1
1
<?php
2
3
namespace Puzzle\AMQP\Workers\Chunks\ChunkAssemblyTrackers;
4
5
use Puzzle\AMQP\Workers\Chunks\ChunkAssemblyTracker;
6
use Puzzle\ValueObjects\Uuid;
7
use Puzzle\AMQP\Messages\Chunks\ChunkedMessageMetadata;
8
9
class MultipleTracker implements ChunkAssemblyTracker
10
{
11
    private
12
        $trackers,
13
        $trackerInstanciator;
14
15
    public function __construct(\Closure $trackerInstanciator)
16
    {
17
        $this->trackers = [];
18
        $this->trackerInstanciator = $trackerInstanciator;
19
    }
20
21
    public function hasBeenStarted(Uuid $uuid)
22
    {
23
        $tracker = $this->getTracker($uuid);
24
25
        return $tracker->hasBeenStarted($uuid);
26
    }
27
28
    private function getTracker(Uuid $uuid)
29
    {
30
        if(! isset($this->trackers[$uuid->value()]))
31
        {
32
            $factory = $this->trackerInstanciator;
33
            $this->trackers[$uuid->value()] = $factory();
34
        }
35
36
        return $this->trackers[$uuid->value()];
37
    }
38
39
    public function start(ChunkedMessageMetadata $metadata)
40
    {
41
        $tracker = $this->getTracker($metadata->uuid());
42
43
        return $tracker->start($metadata);
44
    }
45
46
    public function markAsReceived(Uuid $uuid, $playhead)
47
    {
48
        $tracker = $this->getTracker($uuid);
49
50
        return $tracker->markAsReceived($uuid, $playhead);
51
    }
52
53
    public function isAllHasBeenReceived(Uuid $uuid)
54
    {
55
        $tracker = $this->getTracker($uuid);
56
57
        return $tracker->isAllHasBeenReceived($uuid);
58
    }
59
}
60