BlobEncode::supportsEncoding()   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 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Knik\Binn\Encoder;
4
5
use Knik\Binn\Binn;
6
use Knik\Binn\Contracts\BinnValueEncoder;
7
use Knik\Binn\Exceptions\BinnException;
8
9
class BlobEncode implements BinnValueEncoder
10
{
11
    public const TYPE = Binn::BINN_STORAGE_BLOB;
12
13
    public function encode($value): string
14
    {
15
        if (!$this->supportsEncoding($value)) {
16
            throw new BinnException('Invalid value. Resource expected');
17
        }
18
19
        $contents = '';
20
21
        while (!feof($value)) {
22
            $contents .= fread($value, 1024);
23
        }
24
25
        $encodedType  = Packer::packUint8(self::TYPE);
26
        $encodedSize = Packer::packSize(strlen($encodedType) + strlen($contents), true);
27
28
        return $encodedType . $encodedSize . $contents;
29
    }
30
31
    public function supportsEncoding($value): bool
32
    {
33
        return is_resource($value);
34
    }
35
}
36