Passed
Push — 3.0 ( 5e1ed3...5a5495 )
by Rubén
04:22
created

FileCacheBase::createPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Storage\File;
26
27
28
/**
29
 * Class FileCacheBase
30
 *
31
 * @package SP\Storage\File
32
 */
33
abstract class FileCacheBase implements FileCacheInterface
34
{
35
    /**
36
     * @var FileHandler
37
     */
38
    protected $path;
39
40
    /**
41
     * FileCacheBase constructor.
42
     *
43
     * @param string $path
44
     */
45
    public function __construct($path)
46
    {
47
        $this->path = new FileHandler($path);
48
    }
49
50
    /**
51
     * @param $path
52
     *
53
     * @return FileCacheBase
54
     */
55
    public static function factory($path)
56
    {
57
        return new static($path);
58
    }
59
60
    /**
61
     * Returns if the file is expired adding time to modification date
62
     *
63
     * @param int $time
64
     *
65
     * @return bool
66
     * @throws FileException
67
     */
68
    public function isExpired($time = 86400): bool
69
    {
70
        $this->path->checkFileExists();
71
72
        return time() > $this->path->getFileTime() + $time;
73
    }
74
75
    /**
76
     * Returns if the file is expired adding time to modification date
77
     *
78
     * @param int $date
79
     *
80
     * @return bool
81
     * @throws FileException
82
     */
83
    public function isExpiredDate($date): bool
84
    {
85
        $this->path->checkFileExists();
86
87
        return (int)$date > $this->path->getFileTime();
88
    }
89
90
    /**
91
     * @throws FileException
92
     */
93
    public function createPath()
94
    {
95
        $path = dirname($this->path->getFile());
96
97
        if (!is_dir($path) && mkdir($path, 0700, true) === false) {
98
            throw new FileException(sprintf(__('Unable to create the directory (%s)'), $path));
99
        }
100
    }
101
102
    /**
103
     * @return FileCacheInterface
104
     * @throws FileException
105
     */
106
    public function delete()
107
    {
108
        $this->path->delete();
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function exists(): bool
117
    {
118
        return file_exists($this->path->getFile());
119
    }
120
}