Passed
Pull Request — assets (#722)
by Arnaud
07:45 queued 05:19
created

Cache::preparesHashFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Cache;
12
13
use Cecil\Builder;
14
use Cecil\Config;
15
use Cecil\Util;
16
17
class Cache
18
{
19
    /** @var Builder */
20
    protected $builder;
21
    /** @var Config */
22
    protected $config;
23
    /** @var string */
24
    protected $scope;
25
    /** @var string */
26
    protected $rootPath;
27
28
    /**
29
     * @param Builder $builder
30
     * @param string  $scope
31
     * @param string  $rootPath
32
     */
33
    public function __construct(Builder $builder, string $scope, string $rootPath)
34
    {
35
        $this->builder = $builder;
36
        $this->config = $builder->getConfig();
37
38
        $this->scope = $scope;
39
        $this->rootPath = $rootPath;
40
    }
41
42
    /**
43
     * Returns true if cache entry already exists.
44
     *
45
     * @param string $file
46
     *
47
     * @return bool
48
     */
49
    public function isExists(string $file): bool
50
    {
51
        if (!Util::getFS()->exists($this->getCachePathname($file)) || !Util::getFS()->exists($this->getHashFile($file))) {
52
            return false;
53
        }
54
55
        return true;
56
    }
57
58
    /**
59
     * Returns MD5 hash of $file.
60
     *
61
     * @param string $file
62
     *
63
     * @return string
64
     */
65
    public function getHash(string $file): string
66
    {
67
        return hash_file('md5', $file);
68
    }
69
70
    /**
71
     * Copying cached file and creates hash file.
72
     *
73
     * @param string      $file
74
     * @param string|null $hash
75
     *
76
     * @return void
77
     */
78
    public function save(string $file, string $hash = null): void
79
    {
80
        $this->removesHashFile($this->getRelativePathname($file));
81
        // file
82
        Util::getFS()->copy($file, $this->getCachePathname($file), true);
83
        // hash
84
        Util::getFS()->mkdir(Util::joinFile($this->config->getCachePath(), Util::joinFile($this->scope, 'hash')));
85
        Util::getFS()->touch($this->getHashFile($file, $hash));
86
    }
87
88
    /**
89
     * Returns path to the from from the $rootPath.
90
     *
91
     * @param string $file
92
     *
93
     * @return string
94
     */
95
    private function getRelativePathname(string $file): string
96
    {
97
        $this->relativePath = trim(Util::getFS()->makePathRelative(dirname($file), $this->rootPath), './');
0 ignored issues
show
Bug Best Practice introduced by
The property relativePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
99
        return Util::joinFile($this->relativePath, basename($file));
100
    }
101
102
    /**
103
     * Creates cached file path.
104
     *
105
     * @param string $file
106
     *
107
     * @return string
108
     */
109
    private function getCachePathname($file): string
110
    {
111
        return Util::joinFile(
112
            $this->config->getCachePath(),
113
            Util::joinFile($this->scope, 'files'),
114
            $this->getRelativePathname($file)
115
        );
116
    }
117
118
    /**
119
     * Creates file hash.
120
     *
121
     * @param string      $file
122
     * @param string|null $hash
123
     *
124
     * @return string
125
     */
126
    private function getHashFile(string $file, string $hash = null): string
127
    {
128
        if ($hash === null) {
129
            $hash = $this->getHash($file);
130
        }
131
132
        return Util::joinFile(
133
            $this->config->getCachePath(),
134
            Util::joinFile($this->scope, 'hash'),
135
            $this->preparesHashFile($this->getRelativePathname($file)).$hash
136
        );
137
    }
138
139
    /**
140
     * Prepares hash file path.
141
     *
142
     * @param string $path
143
     *
144
     * @return string
145
     */
146
    private function preparesHashFile(string $path): string
147
    {
148
        return str_replace(DIRECTORY_SEPARATOR, '-', $path).'_';
149
    }
150
151
    /**
152
     * Removes previous hash files.
153
     *
154
     * @param string $path
155
     *
156
     * @return void
157
     */
158
    private function removesHashFile(string $path): void
159
    {
160
        $pattern = Util::joinFile($this->config->getCachePath(), Util::joinFile($this->scope, 'hash'), $this->preparesHashFile($path)).'*';
161
        foreach (glob($pattern) as $filename) {
162
            Util::getFS()->remove($filename);
163
        }
164
    }
165
}
166