ChecksumAdapterTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A checksum() 0 12 2
1
<?php
2
3
namespace diecoding\flysystem\traits;
4
5
use League\Flysystem\Config;
6
use League\Flysystem\UnableToProvideChecksum;
7
8
/**
9
 * Trait ChecksumAdapterTrait for Adapter
10
 * 
11
 * @method string read(string $location)
12
 * 
13
 * @link      https://sugengsulistiyawan.my.id/
14
 * @author    Sugeng Sulistiyawan <[email protected]>
15
 * @copyright Copyright (c) 2023
16
 */
17
trait ChecksumAdapterTrait
18
{
19
    public function checksum(string $path, Config $config): string
20
    {
21
        $algo = $config->get('checksum_algo', 'md5');
22
        $contents = $this->read($path);
23
        error_clear_last();
24
        $checksum = @hash($algo, $contents);
25
26
        if ($checksum === false) {
27
            throw new UnableToProvideChecksum(error_get_last()['message'] ?? '', $path);
28
        }
29
30
        return $checksum;
31
    }
32
}
33