DefaultHashToPathMapper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pathToHash() 0 5 1
A hashToPath() 0 9 2
A __construct() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the Runalyze BSON.
5
 * (c) RUNALYZE <[email protected]>
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Runalyze\BSON\LazyFilesystemObject;
11
12
class DefaultHashToPathMapper implements HashToPathMapperInterface
13
{
14
    /** @var int */
15
    protected $NumberOfDirectories;
16
17
    /** @var int */
18
    protected $CharsPerDirectory;
19
20
    /** @var string */
21
    protected $DirectorySeparator;
22
23
    /**
24
     * @param int    $numberOfDirectories
25
     * @param int    $charsPerDirectory
26
     * @param string $directorySeparator
27
     */
28 4
    public function __construct($numberOfDirectories = 3, $charsPerDirectory = 2, $directorySeparator = '/')
29
    {
30 4
        $this->NumberOfDirectories = $numberOfDirectories;
31 4
        $this->CharsPerDirectory = $charsPerDirectory;
32 4
        $this->DirectorySeparator = $directorySeparator;
33 4
    }
34
35
    /**
36
     * @param  string $hash
37
     * @return string file path
38
     */
39 4
    public function hashToPath($hash)
40
    {
41 4
        $prefix = '';
42
43 4
        for ($i = 0; $i < $this->NumberOfDirectories; ++$i) {
44 4
            $prefix .= substr($hash, $i * $this->CharsPerDirectory, $this->CharsPerDirectory).$this->DirectorySeparator;
45
        }
46
47 4
        return $prefix.$hash;
48
    }
49
50
    /**
51
     * @param  string $filePath
52
     * @return string hash
53
     */
54 3
    public function pathToHash($filePath)
55
    {
56 3
        $start = $this->NumberOfDirectories * ($this->CharsPerDirectory + strlen($this->DirectorySeparator));
57
58 3
        return substr($filePath, $start);
59
    }
60
}
61