1 | <?php |
||
16 | class FileStorage extends Storage |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $dir; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $gcProbability; |
||
27 | |||
28 | /** |
||
29 | * @const string |
||
30 | */ |
||
31 | const FILE_SUFFIX = '.phpsst'; |
||
32 | |||
33 | /** |
||
34 | * FileStorage constructor. |
||
35 | * @param string $dir |
||
36 | * @param int $gcProbability |
||
37 | */ |
||
38 | 10 | public function __construct($dir, $gcProbability) |
|
39 | { |
||
40 | 10 | $dir = rtrim($dir, '/') . '/'; |
|
41 | 10 | if (empty($dir) || !is_dir($dir)) { |
|
42 | 1 | throw new \RuntimeException('Invalid directory path'); |
|
43 | } |
||
44 | |||
45 | 9 | if ($gcProbability < 0) { |
|
46 | 1 | throw new \LogicException('Invalid value for gcProbability'); |
|
47 | } |
||
48 | |||
49 | 8 | $this->dir = $dir; |
|
50 | 8 | $this->gcProbability = $gcProbability; |
|
51 | 8 | } |
|
52 | |||
53 | /** |
||
54 | * @param Password $password |
||
55 | * @param bool $allowOverwrite |
||
56 | */ |
||
57 | 8 | public function store(Password $password, $allowOverwrite = false) |
|
65 | |||
66 | /** |
||
67 | * @param $key |
||
68 | * @return Password|null |
||
69 | */ |
||
70 | 3 | public function get($key) |
|
80 | |||
81 | /** |
||
82 | * @param Password $password |
||
83 | */ |
||
84 | 2 | public function delete(Password $password) |
|
88 | |||
89 | /** |
||
90 | */ |
||
91 | 6 | protected function garbageCollection() |
|
106 | |||
107 | /** |
||
108 | * @param Password $password |
||
109 | */ |
||
110 | 7 | protected function writeFile(Password $password) |
|
121 | |||
122 | /** |
||
123 | * @param Password $password |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | protected function getFileName(Password $password) |
|
130 | |||
131 | /** |
||
132 | * @param string $key |
||
133 | * @return string |
||
134 | */ |
||
135 | 4 | protected function getFileNameFromKey($key) |
|
139 | } |
||
140 |