| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 0.00% | 0 / 18 |  | 0.00% | 0 / 3 | CRAP |  | 0.00% | 0 / 1 | 
| SetupFileCache |  | 0.00% | 0 / 18 |  | 0.00% | 0 / 3 | 42 |  | 0.00% | 0 / 1 | 
| createDirectory |  | 0.00% | 0 / 11 |  | 0.00% | 0 / 1 | 20 | |||
| createPersistence |  | 0.00% | 0 / 6 |  | 0.00% | 0 / 1 | 2 | |||
| getPersistenceManager |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| 1 | <?php | 
| 2 | |
| 3 | /** | 
| 4 | * This program is free software; you can redistribute it and/or | 
| 5 | * modify it under the terms of the GNU General Public License | 
| 6 | * as published by the Free Software Foundation; under version 2 | 
| 7 | * of the License (non-upgradable). | 
| 8 | * | 
| 9 | * This program is distributed in the hope that it will be useful, | 
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 | * GNU General Public License for more details. | 
| 13 | * | 
| 14 | * You should have received a copy of the GNU General Public License | 
| 15 | * along with this program; if not, write to the Free Software | 
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
| 17 | * | 
| 18 | * Copyright (c) 2022 (original work) Open Assessment Technologies SA ; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\oatbox\cache; | 
| 24 | |
| 25 | use Exception; | 
| 26 | use oat\generis\persistence\PersistenceManager; | 
| 27 | use oat\oatbox\service\ConfigurableService; | 
| 28 | |
| 29 | class SetupFileCache extends ConfigurableService | 
| 30 | { | 
| 31 | public const PERSISTENCE = 'cache'; | 
| 32 | |
| 33 | /** | 
| 34 | * @throws Exception | 
| 35 | */ | 
| 36 | public function createDirectory($cachePath): void | 
| 37 | { | 
| 38 | if (is_dir($cachePath) && is_writable($cachePath)) { | 
| 39 | return; | 
| 40 | } | 
| 41 | |
| 42 | if (!@mkdir($cachePath, 0700, true)) { | 
| 43 | $error = error_get_last(); | 
| 44 | throw new Exception( | 
| 45 | sprintf( | 
| 46 | 'Could not create application cache at "%s". Error message: %s', | 
| 47 | $cachePath, | 
| 48 | $error['message'] ?? 'unknown' | 
| 49 | ) | 
| 50 | ); | 
| 51 | } | 
| 52 | } | 
| 53 | |
| 54 | public function createPersistence(): void | 
| 55 | { | 
| 56 | $persistenceManager = $this->getPersistenceManager(); | 
| 57 | $persistenceManager->registerPersistence(self::PERSISTENCE, [ | 
| 58 | 'driver' => 'phpfile' | 
| 59 | ]); | 
| 60 | $persistenceManager->getPersistenceById(self::PERSISTENCE)->purge(); | 
| 61 | $this->getServiceManager()->register(PersistenceManager::SERVICE_ID, $persistenceManager); | 
| 62 | } | 
| 63 | |
| 64 | private function getPersistenceManager(): PersistenceManager | 
| 65 | { | 
| 66 | return $this->getServiceManager()->get(PersistenceManager::SERVICE_ID); | 
| 67 | } | 
| 68 | } |