Completed
Push — master ( 2a17e7...e610ee )
by Arne
04:45
created

HashProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 5.06 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 4
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getHash() 0 11 2
A loadHashes() 4 15 2
B doLoadHashes() 0 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Storeman\Hash;
4
5
use Storeman\Config\Configuration;
6
use Storeman\FileReader;
7
use Storeman\Hash\Algorithm\HashAlgorithmInterface;
8
use Storeman\Index\IndexObject;
9
10
final class HashProvider
11
{
12
    /**
13
     * @var FileReader
14
     */
15
    protected $fileReader;
16
17
    /**
18
     * @var Configuration
19
     */
20
    protected $configuration;
21
22
    /**
23
     * @var HashAlgorithmInterface[]
24
     */
25
    protected $algorithms = [];
26
27
    public function __construct(FileReader $fileReader, Configuration $configuration, array $algorithms)
28
    {
29
        $this->fileReader = $fileReader;
30
        $this->configuration = $configuration;
31
        $this->algorithms = $algorithms;
32
    }
33
34
    public function getHash(IndexObject $indexObject, string $algorithm): string
35
    {
36
        $hashes = $indexObject->getHashes();
37
38
        if (!$hashes->hasHash($algorithm))
39
        {
40
            $this->loadHashes($indexObject, [$algorithm]);
41
        }
42
43
        return $hashes->getHash($algorithm);
44
    }
45
46
    public function loadHashes(IndexObject $indexObject, array $algorithms): void
47
    {
48
        // prevent computation of already known hashes
49
        $algorithms = array_diff($algorithms, array_keys(iterator_to_array($indexObject->getHashes())));
50
51 View Code Duplication
        if ($unknownAlgorithms = array_diff($algorithms, array_keys($this->algorithms)))
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...
52
        {
53
            throw new \InvalidArgumentException(sprintf('Unknown algorithm(s): %s', implode(',', $unknownAlgorithms)));
54
        }
55
56
        // prevent re-computation of hashes that are build on file read anyway
57
        $missingAlgorithms = array_diff($algorithms, $this->configuration->getFileChecksums());
58
59
        $this->doLoadHashes($indexObject, $missingAlgorithms);
60
    }
61
62
    protected function doLoadHashes(IndexObject $indexObject, array $algorithms): void
63
    {
64
        $hashFunction = new AggregateHashAlgorithm(array_intersect_key($this->algorithms, array_flip($algorithms)));
65
        $hashFunction->initialize();
66
67
        $fileHandle = $this->fileReader->getReadStream($indexObject);
68
69
        while (!feof($fileHandle))
70
        {
71
            $buffer = fread($fileHandle, 65536);
72
73
            if ($buffer === false)
74
            {
75
                throw new \RuntimeException("fread() failed");
76
            }
77
78
            $hashFunction->digest($buffer);
79
        }
80
81
        foreach ($hashFunction->finalize() as $algorithm => $hash)
82
        {
83
            $indexObject->getHashes()->addHash($algorithm, $hash);
84
        }
85
86
        fclose($fileHandle);
87
    }
88
}
89