Passed
Push — master ( ddf6f5...2e933f )
by Nicolas
03:42 queued 28s
created

StreamedBinary::asTransported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 11
Ratio 57.89 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 11
loc 19
ccs 14
cts 14
cp 1
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 12
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Puzzle\AMQP\Messages\Bodies;
4
5
use Puzzle\ValueObjects\Uuid;
6
use Puzzle\AMQP\Messages\Chunks\Chunk;
7
use Puzzle\AMQP\Messages\Chunks\ChunkSize;
8
use Puzzle\AMQP\Messages\Chunks\ChunkedMessageMetadata;
9
10
class StreamedBinary extends Binary
11
{
12
    private
13
        $chunkSize,
14
        $metadata;
15
16 2
    public function __construct($content, ChunkSize $chunkSize)
17
    {
18 2
        parent::__construct($content);
19
20 2
        $this->chunkSize = $chunkSize;
21
22 2
        $size = strlen($content);
23 2
        $nbChunks = (int) ceil($size / $chunkSize->toBytes());
24
25 2
        $this->metadata = new ChunkedMessageMetadata(new Uuid(), $size, $nbChunks, sha1($content));
26 2
    }
27
28 3
    public function asTransported()
29
    {
30 3
        $wholeContent = parent::asTransported();
31 3
        $length = strlen($wholeContent);
32 3
        $offset = 0;
33 3
        $playhead = 0;
34
35 3 View Code Duplication
        while($offset < $length)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
        {
37 3
            $content = substr($wholeContent, $offset, $this->chunkSize->toBytes());
38 3
            $playhead++;
39
40 3
            $chunk = new Chunk($playhead, $offset, $content, $this->metadata);
41 3
            yield $chunk;
42
43 3
            $offset += $chunk->size();
44 3
            unset($chunk, $content);
45 3
        }
46 3
    }
47
48 1
    public function isChunked()
49
    {
50 1
        return true;
51
    }
52
}
53