diff options
Diffstat (limited to 'apps/files_versions/lib/Expiration.php')
-rw-r--r-- | apps/files_versions/lib/Expiration.php | 97 |
1 files changed, 49 insertions, 48 deletions
diff --git a/apps/files_versions/lib/Expiration.php b/apps/files_versions/lib/Expiration.php index 1dad8801230..1e04d93379f 100644 --- a/apps/files_versions/lib/Expiration.php +++ b/apps/files_versions/lib/Expiration.php @@ -1,37 +1,20 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Victor Dubiniuk <dubiniuk@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Versions; -use \OCP\IConfig; -use \OCP\AppFramework\Utility\ITimeFactory; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use Psr\Log\LoggerInterface; class Expiration { // how long do we keep files a version if no other value is defined in the config file (unit: days) - const NO_OBLIGATION = -1; - - /** @var ITimeFactory */ - private $timeFactory; + public const NO_OBLIGATION = -1; /** @var string */ private $retentionObligation; @@ -45,8 +28,11 @@ class Expiration { /** @var bool */ private $canPurgeToSaveSpace; - public function __construct(IConfig $config,ITimeFactory $timeFactory){ - $this->timeFactory = $timeFactory; + public function __construct( + IConfig $config, + private ITimeFactory $timeFactory, + private LoggerInterface $logger, + ) { $this->retentionObligation = $config->getSystemValue('versions_retention_obligation', 'auto'); if ($this->retentionObligation !== 'disabled') { @@ -58,14 +44,14 @@ class Expiration { * Is versions expiration enabled * @return bool */ - public function isEnabled(){ + public function isEnabled(): bool { return $this->retentionObligation !== 'disabled'; } /** * Is default expiration active */ - public function shouldAutoExpire(){ + public function shouldAutoExpire(): bool { return $this->minAge === self::NO_OBLIGATION || $this->maxAge === self::NO_OBLIGATION; } @@ -76,7 +62,7 @@ class Expiration { * @param bool $quotaExceeded * @return bool */ - public function isExpired($timestamp, $quotaExceeded = false){ + public function isExpired(int $timestamp, bool $quotaExceeded = false): bool { // No expiration if disabled if (!$this->isEnabled()) { return false; @@ -90,7 +76,7 @@ class Expiration { $time = $this->timeFactory->getTime(); // Never expire dates in future e.g. misconfiguration or negative time // adjustment - if ($time<$timestamp) { + if ($time < $timestamp) { return false; } @@ -114,10 +100,25 @@ class Expiration { } /** + * Get minimal retention obligation as a timestamp + * + * @return int|false + */ + public function getMinAgeAsTimestamp() { + $minAge = false; + if ($this->isEnabled() && $this->minAge !== self::NO_OBLIGATION) { + $time = $this->timeFactory->getTime(); + $minAge = $time - ($this->minAge * 86400); + } + return $minAge; + } + + /** * Get maximal retention obligation as a timestamp - * @return int + * + * @return int|false */ - public function getMaxAgeAsTimestamp(){ + public function getMaxAgeAsTimestamp() { $maxAge = false; if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) { $time = $this->timeFactory->getTime(); @@ -127,10 +128,10 @@ class Expiration { } /** - * Read versions_retention_obligation, validate it - * and set private members accordingly - */ - private function parseRetentionObligation(){ + * Read versions_retention_obligation, validate it + * and set private members accordingly + */ + private function parseRetentionObligation(): void { $splitValues = explode(',', $this->retentionObligation); if (!isset($splitValues[0])) { $minValue = 'auto'; @@ -148,21 +149,21 @@ class Expiration { // Validate if (!ctype_digit($minValue) && $minValue !== 'auto') { $isValid = false; - \OC::$server->getLogger()->warning( - $minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.', - ['app'=>'files_versions'] + $this->logger->warning( + $minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.', + ['app' => 'files_versions'] ); } if (!ctype_digit($maxValue) && $maxValue !== 'auto') { $isValid = false; - \OC::$server->getLogger()->warning( - $maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.', - ['app'=>'files_versions'] + $this->logger->warning( + $maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.', + ['app' => 'files_versions'] ); } - if (!$isValid){ + if (!$isValid) { $minValue = 'auto'; $maxValue = 'auto'; } @@ -175,13 +176,13 @@ class Expiration { $this->canPurgeToSaveSpace = true; } elseif ($minValue !== 'auto' && $maxValue === 'auto') { // Keep for X days but delete anytime if space needed - $this->minAge = intval($minValue); + $this->minAge = (int)$minValue; $this->maxAge = self::NO_OBLIGATION; $this->canPurgeToSaveSpace = true; } elseif ($minValue === 'auto' && $maxValue !== 'auto') { // Delete anytime if space needed, Delete all older than max automatically $this->minAge = self::NO_OBLIGATION; - $this->maxAge = intval($maxValue); + $this->maxAge = (int)$maxValue; $this->canPurgeToSaveSpace = true; } elseif ($minValue !== 'auto' && $maxValue !== 'auto') { // Delete all older than max OR older than min if space needed @@ -191,8 +192,8 @@ class Expiration { $maxValue = $minValue; } - $this->minAge = intval($minValue); - $this->maxAge = intval($maxValue); + $this->minAge = (int)$minValue; + $this->maxAge = (int)$maxValue; $this->canPurgeToSaveSpace = false; } } |