Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.36% |
38 / 44 |
|
68.75% |
11 / 16 |
CRAP | |
0.00% |
0 / 1 |
AppDatabaseCache | |
86.36% |
38 / 44 |
|
68.75% |
11 / 16 |
18.82 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
openDatabase | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
exportCache | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
readCache | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
existsCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
clearCache | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
exportUserInformation | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
exportPluginInformation | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
exportAll | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
existsPluginInformation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readPluginInformation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clearPluginInformation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
existsUserInformation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readUserInformation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
clearUserInformation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clearAll | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models; |
6 | |
7 | use PeServer\App\Models\Cache\CacheException; |
8 | use PeServer\App\Models\Cache\PluginCache; |
9 | use PeServer\App\Models\Cache\PluginCacheCategory; |
10 | use PeServer\App\Models\Cache\PluginCacheItem; |
11 | use PeServer\App\Models\Cache\UserCache; |
12 | use PeServer\App\Models\Dao\Domain\PluginDomainDao; |
13 | use PeServer\App\Models\Dao\Domain\UserDomainDao; |
14 | use PeServer\Core\Binary; |
15 | use PeServer\Core\Database\IDatabaseConnection; |
16 | use PeServer\Core\Database\IDatabaseContext; |
17 | use PeServer\Core\IO\Directory; |
18 | use PeServer\Core\IO\File; |
19 | use PeServer\Core\IO\Path; |
20 | use PeServer\Core\Serialization\BuiltinSerializer; |
21 | use PeServer\Core\Serialization\ISerializer; |
22 | use PeServer\Core\Serialization\SerializerBase; |
23 | use PeServer\Core\Throws\SerializeException; |
24 | use PeServer\Core\TypeUtility; |
25 | |
26 | class AppDatabaseCache |
27 | { |
28 | #region define |
29 | |
30 | private const USER_INFORMATION = 'user.cache'; |
31 | private const PLUGIN_INFORMATION = 'plugin.cache'; |
32 | |
33 | #endregion |
34 | |
35 | #region variable |
36 | |
37 | private string $cacheDirectoryPath; |
38 | private ISerializer $serializer; |
39 | |
40 | #endregion |
41 | |
42 | public function __construct( |
43 | AppConfiguration $config, |
44 | private IDatabaseConnection $connection |
45 | ) { |
46 | $this->cacheDirectoryPath = $config->setting->cache->database; |
47 | $this->serializer = new BuiltinSerializer(); |
48 | } |
49 | |
50 | #region function |
51 | |
52 | private function openDatabase(): IDatabaseContext |
53 | { |
54 | return $this->connection->open(); |
55 | } |
56 | |
57 | /** |
58 | * オブジェクトをキャシュデータとして出力。 |
59 | * |
60 | * @param string $fileName |
61 | * @param object $object |
62 | * @return void |
63 | */ |
64 | private function exportCache(string $fileName, object $object): void |
65 | { |
66 | $filePath = Path::combine($this->cacheDirectoryPath, $fileName); |
67 | Directory::createParentDirectoryIfNotExists($filePath); |
68 | |
69 | $binary = $this->serializer->save($object); |
70 | File::writeContent($filePath, $binary); |
71 | } |
72 | |
73 | /** |
74 | * キャシュデータをオブジェクトとして読み込み。 |
75 | * |
76 | * @template T of object |
77 | * @param string $fileName |
78 | * @param string $className |
79 | * @phpstan-param class-string<T> $className |
80 | * @return object |
81 | * @phpstan-return T |
82 | */ |
83 | private function readCache(string $fileName, string $className): object |
84 | { |
85 | $filePath = Path::combine($this->cacheDirectoryPath, $fileName); |
86 | $binary = File::readContent($filePath); |
87 | $object = $this->serializer->load($binary); |
88 | if (!is_array($object) && is_a($object, $className)) { |
89 | /** @phpstan-var T */ |
90 | return $object; |
91 | } |
92 | |
93 | throw new SerializeException(TypeUtility::getType($object)); |
94 | } |
95 | |
96 | private function existsCache(string $fileName): bool |
97 | { |
98 | $filePath = Path::combine($this->cacheDirectoryPath, $fileName); |
99 | return File::exists($filePath); |
100 | } |
101 | |
102 | private function clearCache(string $fileName): void |
103 | { |
104 | $filePath = Path::combine($this->cacheDirectoryPath, $fileName); |
105 | File::removeFileIfExists($filePath); |
106 | } |
107 | |
108 | |
109 | /** |
110 | * ユーザー情報をキャッシュ出力。 |
111 | * |
112 | * @return void |
113 | */ |
114 | public function exportUserInformation(): void |
115 | { |
116 | $context = $this->openDatabase(); |
117 | $userDomainDao = new UserDomainDao($context); |
118 | |
119 | $cache = new UserCache( |
120 | $userDomainDao->selectCacheItems() |
121 | ); |
122 | |
123 | self::exportCache(self::USER_INFORMATION, $cache); |
124 | } |
125 | |
126 | /** |
127 | * プラグイン情報をキャッシュ出力。 |
128 | * |
129 | * @return void |
130 | */ |
131 | public function exportPluginInformation(): void |
132 | { |
133 | $context = $this->openDatabase(); |
134 | $userDomainDao = new PluginDomainDao($context); |
135 | |
136 | $cache = new PluginCache( |
137 | $userDomainDao->selectCacheCategories(), |
138 | $userDomainDao->selectCacheItems() |
139 | ); |
140 | |
141 | self::exportCache(self::PLUGIN_INFORMATION, $cache); |
142 | } |
143 | |
144 | /** |
145 | * キャッシュを全出力。 |
146 | * |
147 | * @return string[] |
148 | */ |
149 | public function exportAll(): array |
150 | { |
151 | $this->exportUserInformation(); |
152 | $this->exportPluginInformation(); |
153 | |
154 | return [ |
155 | 'user_information', |
156 | 'plugin_information', |
157 | ]; |
158 | } |
159 | |
160 | /** |
161 | * プラグイン情報のキャッシュファイルが存在するか。 |
162 | */ |
163 | public function existsPluginInformation(): bool |
164 | { |
165 | return $this->existsCache(self::PLUGIN_INFORMATION); |
166 | } |
167 | |
168 | /** |
169 | * プラグイン情報のキャッシュ取得。 |
170 | * |
171 | * @return PluginCache |
172 | */ |
173 | public function readPluginInformation(): PluginCache |
174 | { |
175 | return $this->readCache(self::PLUGIN_INFORMATION, PluginCache::class); |
176 | } |
177 | |
178 | private function clearPluginInformation(): void |
179 | { |
180 | $this->clearCache(self::PLUGIN_INFORMATION); |
181 | } |
182 | |
183 | /** |
184 | * プラグイン情報のキャッシュファイルが存在するか。 |
185 | */ |
186 | public function existsUserInformation(): bool |
187 | { |
188 | return $this->existsCache(self::USER_INFORMATION); |
189 | } |
190 | |
191 | /** |
192 | * ユーザー情報のキャッシュ取得。 |
193 | * |
194 | * @return UserCache |
195 | */ |
196 | public function readUserInformation(): UserCache |
197 | { |
198 | return $this->readCache(self::USER_INFORMATION, UserCache::class); |
199 | } |
200 | |
201 | private function clearUserInformation(): void |
202 | { |
203 | $this->clearCache(self::USER_INFORMATION); |
204 | } |
205 | |
206 | |
207 | public function clearAll(): void |
208 | { |
209 | $this->clearPluginInformation(); |
210 | $this->clearUserInformation(); |
211 | } |
212 | |
213 | #endregion |
214 | } |