FileCacheManager::directoryExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\CacheManager;
4
5
use RecursiveDirectoryIterator;
6
use RecursiveIteratorIterator;
7
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException;
8
9
/**
10
 * Class FileCacheManager
11
 * @author Sílvio Silva <https://github.com/silviooosilva>
12
 * @package Silviooosilva\CacheerPhp
13
 */
14
class FileCacheManager
15
{
16
17
    /**
18
     * @param string $dir
19
     * @return void
20
     * @throws CacheFileException
21
     */
22
    public function createDirectory(string $dir): void
23
    {
24
        if ((!file_exists($dir) || !is_dir($dir)) && !mkdir($dir, 0755, true)) {
25
            throw CacheFileException::create("Could not create directory: {$dir}");
26
        }
27
    }
28
29
    /**
30
     * @param string $filename
31
     * @param string $data
32
     * @return void
33
     * @throws CacheFileException
34
     */
35
    public function writeFile(string $filename, string $data): void
36
    {
37
        if (!@file_put_contents($filename, $data, LOCK_EX)) {
38
            throw CacheFileException::create("Could not write file: {$filename}");
39
        }
40
    }
41
42
    /**
43
     * @param string $filename
44
     * @return string|bool
45
     * @throws CacheFileException
46
     */
47
    public function readFile(string $filename): string|bool
48
    {
49
        if (!$this->fileExists($filename)) {
50
            throw CacheFileException::create("File not found: {$filename}");
51
        }
52
        return file_get_contents($filename);
53
    }
54
55
    /**
56
    * @param string $filename
57
    * @return bool
58
    */
59
    public function fileExists(string $filename): bool
60
    {
61
        return file_exists($filename);
62
    }
63
64
    /**
65
    * @param string $filename
66
    * @return void
67
    */
68
    public function removeFile(string $filename): void
69
    {
70
        if (file_exists($filename)) {
71
            unlink($filename);
72
        }
73
    }
74
75
    /**
76
    * @param string $dir
77
    * @return void
78
    */
79
    public function clearDirectory(string $dir): void
80
    {
81
        $iterator = new RecursiveIteratorIterator(
82
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
83
            RecursiveIteratorIterator::CHILD_FIRST
84
        );
85
        foreach ($iterator as $file) {
86
            $path = $file->getPathname();
87
            $file->isDir() ? rmdir($path) : unlink($path);
88
        }
89
    }
90
91
    /**
92
     * @param mixed $data
93
     * @param bool $serialize
94
     * @return mixed|string
95
     */
96
    public function serialize(mixed $data, bool $serialize = true): mixed
97
    {
98
        if($serialize) {
99
            return serialize($data);
100
        }
101
        return unserialize($data);
102
    }
103
104
    /**
105
     * @param string $dir
106
     * @return array
107
     * @throws CacheFileException
108
     */
109
    public function getFilesInDirectory(string $dir): array
110
    {
111
        if (!is_dir($dir)) {
112
            throw CacheFileException::create("Directory does not exist: {$dir}");
113
        }
114
115
        $files = [];
116
        $iterator = new RecursiveIteratorIterator(
117
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS)
118
        );
119
120
        foreach ($iterator as $file) {
121
            if ($file->isFile()) {
122
                $files[] = $file->getPathname();
123
            }
124
        }
125
126
        return $files;
127
    }
128
129
    /**
130
     * @param string $dir
131
     * @return bool
132
     */
133
    public function directoryExists(string $dir): bool
134
    {
135
        return is_dir($dir);
136
    }
137
}
138