Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CacheController | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| clearAll | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| clear | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getCacheInfo | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getLastClearedTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| updateLastClearedTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Controller\Admin; |
| 5 | |
| 6 | use App\Controller\AppController; |
| 7 | use App\Utility\SettingsManager; |
| 8 | use Cake\Cache\Cache; |
| 9 | use Cake\Http\Response; |
| 10 | use DateTime; |
| 11 | |
| 12 | /** |
| 13 | * CacheController |
| 14 | * |
| 15 | * Handles cache clearing operations for the application. |
| 16 | */ |
| 17 | class CacheController extends AppController |
| 18 | { |
| 19 | /** |
| 20 | * Clears all cache configurations and updates the last cleared time. |
| 21 | * |
| 22 | * @return \Cake\Http\Response|null Redirects to the clearAll action or renders the view. |
| 23 | */ |
| 24 | public function clearAll(): ?Response |
| 25 | { |
| 26 | $cacheInfo = $this->getCacheInfo(); |
| 27 | |
| 28 | if ($this->request->is('post')) { |
| 29 | $clearedCaches = []; |
| 30 | $failedCaches = []; |
| 31 | |
| 32 | foreach ($cacheInfo as $config => $info) { |
| 33 | if (Cache::clear($config)) { |
| 34 | $clearedCaches[] = $config; |
| 35 | $this->updateLastClearedTime($config); |
| 36 | } else { |
| 37 | $failedCaches[] = $config; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | SettingsManager::clearCache(); |
| 42 | $clearedCaches[] = 'SettingsManager'; |
| 43 | |
| 44 | if (empty($failedCaches)) { |
| 45 | $this->Flash->success(__('All caches have been cleared successfully.')); |
| 46 | } else { |
| 47 | $this->Flash->warning(__( |
| 48 | 'Some caches were cleared, but the following failed: {0}', |
| 49 | implode(', ', $failedCaches), |
| 50 | )); |
| 51 | } |
| 52 | |
| 53 | return $this->redirect(['action' => 'clearAll']); |
| 54 | } |
| 55 | |
| 56 | $this->set('cacheInfo', $cacheInfo); |
| 57 | |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Clears a specific cache configuration and updates the last cleared time. |
| 63 | * |
| 64 | * @param string $cacheName The name of the cache configuration to clear. |
| 65 | * @return \Cake\Http\Response Redirects to the clearAll action. |
| 66 | */ |
| 67 | public function clear(string $cacheName): Response |
| 68 | { |
| 69 | $decodedCacheName = urldecode($cacheName); |
| 70 | |
| 71 | if ($this->request->is('post')) { |
| 72 | if (Cache::getConfig($decodedCacheName)) { |
| 73 | if (Cache::clear($decodedCacheName)) { |
| 74 | $this->updateLastClearedTime($decodedCacheName); |
| 75 | $this->Flash->success(__('{0} cache has been cleared successfully.', ucfirst($decodedCacheName))); |
| 76 | } else { |
| 77 | $this->Flash->error(__('{0} cache could not be cleared.', ucfirst($decodedCacheName))); |
| 78 | } |
| 79 | } else { |
| 80 | $this->Flash->error(__('{0} cache configuration does not exist.', ucfirst($decodedCacheName))); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return $this->redirect(['action' => 'clearAll']); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retrieves information about all configured cache engines. |
| 89 | * |
| 90 | * @return array An array containing cache configuration details. |
| 91 | */ |
| 92 | private function getCacheInfo(): array |
| 93 | { |
| 94 | $cacheInfo = []; |
| 95 | $configuredCaches = Cache::configured(); |
| 96 | |
| 97 | foreach ($configuredCaches as $config) { |
| 98 | $engineConfig = Cache::getConfig($config); |
| 99 | unset($engineConfig['password']); |
| 100 | |
| 101 | $cacheInfo[$config] = [ |
| 102 | 'engine' => $engineConfig['className'], |
| 103 | 'settings' => $engineConfig, |
| 104 | 'last_cleared' => $this->getLastClearedTime($config), |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | return $cacheInfo; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Gets the last cleared time for a specific cache configuration. |
| 113 | * |
| 114 | * @param string $config The cache configuration name. |
| 115 | * @return \DateTime|null The last cleared time or null if not available. |
| 116 | */ |
| 117 | private function getLastClearedTime(string $config): ?DateTime |
| 118 | { |
| 119 | $time = Cache::read('last_cleared_' . $config, 'default'); |
| 120 | |
| 121 | return $time ? new DateTime($time) : null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Updates the last cleared time for a specific cache configuration. |
| 126 | * |
| 127 | * @param string $config The cache configuration name. |
| 128 | * @return void |
| 129 | */ |
| 130 | private function updateLastClearedTime(string $config): void |
| 131 | { |
| 132 | $time = new DateTime(); |
| 133 | Cache::write('last_cleared_' . $config, $time->format('Y-m-d H:i:s'), 'default'); |
| 134 | } |
| 135 | } |