Completed
Push — master ( 422878...4f5158 )
by Nikolay
19s
created

FileSystemHash::hashPath()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 3
nop 1
dl 0
loc 25
rs 9.4888
c 0
b 0
f 0
1
<?php
2
namespace Intersvyaz\AssetManager;
3
4
use RecursiveDirectoryIterator;
5
use RecursiveIteratorIterator;
6
use SplFileInfo;
7
use Yii;
8
9
/**
10
 * Возможность получение хэша по объектам файловой системы.
11
 */
12
class FileSystemHash implements FileSystemHashInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function hashPath($path)
18
    {
19
        $path = realpath($path);
20
        $hashes = [];
21
22
        if (is_dir($path)) {
23
            $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
24
25
            /** @var SplFileInfo $file */
26
            foreach ($it as $file) {
27
                if ($file->isFile()) {
28
                    $hashes[] = md5_file($file) . '-' . $this->hashPathName((string)$file);
29
                }
30
            }
31
32
            if (empty($hashes)) {
33
                $hashes[] = 'null-' . $this->hashPathName($path);
34
            } else {
35
                sort($hashes);
36
            }
37
        } else {
38
            $hashes[] = md5_file($path) . '-' . $this->hashPathName((string)$path);
39
        }
40
41
        return md5(implode($hashes, '|'));
0 ignored issues
show
Bug introduced by
'|' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return md5(implode($hashes, /** @scrutinizer ignore-type */ '|'));
Loading history...
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function hashPathName($path)
48
    {
49
        $hashPath = str_replace(Yii::getAlias('@app'), '', $path);
50
51
        if (DIRECTORY_SEPARATOR === '\\') {
52
            $hashPath = str_replace(DIRECTORY_SEPARATOR, '/', $hashPath);
53
        }
54
55
        return md5($hashPath);
56
    }
57
}
58