DefaultHashToPathMapper::pathToHash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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