Completed
Push — stable8.2 ( 91aa1b...3f16fa )
by Morris
100:33
created

Expiration::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 8
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * @author Lukas Reschke <[email protected]>
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Files_Trashbin;
24
25
use \OCP\IConfig;
26
use \OCP\AppFramework\Utility\ITimeFactory;
27
28
class Expiration {
29
30
	// how long do we keep files in the trash bin if no other value is defined in the config file (unit: days)
31
	const DEFAULT_RETENTION_OBLIGATION = 30;
32
	const NO_OBLIGATION = -1;
33
34
	/** @var ITimeFactory */
35
	private $timeFactory;
36
37
	/** @var string */
38
	private $retentionObligation;
39
40
	/** @var int */
41
	private $minAge;
42
43
	/** @var int */
44
	private $maxAge;
45
46
	/** @var bool */
47
	private $canPurgeToSaveSpace;
48
49 168 View Code Duplication
	public function __construct(IConfig $config,ITimeFactory $timeFactory){
50 168
		$this->timeFactory = $timeFactory;
51 168
		$this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto');
52
53 168
		if ($this->retentionObligation !== 'disabled') {
54 163
			$this->parseRetentionObligation();
55 163
		}
56 168
	}
57
58
	/**
59
	 * Is trashbin expiration enabled
60
	 * @return bool
61
	 */
62 141
	public function isEnabled(){
63 141
		return $this->retentionObligation !== 'disabled';
64
	}
65
66
	/**
67
	 * Check if given timestamp in expiration range
68
	 * @param int $timestamp
69
	 * @param bool $quotaExceeded
70
	 * @return bool
71
	 */
72 39 View Code Duplication
	public function isExpired($timestamp, $quotaExceeded = false){
73
		// No expiration if disabled
74 39
		if (!$this->isEnabled()) {
75 3
			return false;
76
		}
77
78
		// Purge to save space (if allowed)
79 36
		if ($quotaExceeded && $this->canPurgeToSaveSpace) {
80 12
			return true;
81
		}
82
83 24
		$time = $this->timeFactory->getTime();
84
		// Never expire dates in future e.g. misconfiguration or negative time
85
		// adjustment
86 24
		if ($time<$timestamp) {
87 2
			return false;
88
		}
89
90
		// Purge as too old
91 22
		if ($this->maxAge !== self::NO_OBLIGATION) {
92 16
			$maxTimestamp = $time - ($this->maxAge * 86400);
93 16
			$isOlderThanMax = $timestamp < $maxTimestamp;
94 16
		} else {
95 6
			$isOlderThanMax = false;
96
		}
97
98 22
		if ($this->minAge !== self::NO_OBLIGATION) {
99
			// older than Min obligation and we are running out of quota?
100 18
			$minTimestamp = $time - ($this->minAge * 86400);
101 18
			$isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded;
102 18
		} else {
103 4
			$isMinReached = false;
104
		}
105
106 22
		return $isOlderThanMax || $isMinReached;
107
	}
108
109
	/**
110
	 * @return bool|int
111
	 */
112 8 View Code Duplication
	public function getMaxAgeAsTimestamp() {
113 8
		$maxAge = false;
114 8
		if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) {
115 3
			$time = $this->timeFactory->getTime();
116 3
			$maxAge = $time - ($this->maxAge * 86400);
117 3
		}
118 8
		return $maxAge;
119
	}
120
121 163
	private function parseRetentionObligation(){
122 163
		$splitValues = explode(',', $this->retentionObligation);
123 163 View Code Duplication
		if (!isset($splitValues[0])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
			$minValue = self::DEFAULT_RETENTION_OBLIGATION;
125
		} else {
126 163
			$minValue = trim($splitValues[0]);
127
		}
128
129 163
		if (!isset($splitValues[1]) && $minValue === 'auto') {
130 104
			$maxValue = 'auto';
131 163
		} elseif (!isset($splitValues[1])) {
132 8
			$maxValue = self::DEFAULT_RETENTION_OBLIGATION;
133 8
		} else {
134 51
			$maxValue = trim($splitValues[1]);
135
		}
136
137 163 View Code Duplication
		if ($minValue === 'auto' && $maxValue === 'auto') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
			// Default: Keep for 30 days but delete anytime if space needed
139 112
			$this->minAge = self::DEFAULT_RETENTION_OBLIGATION;
140 112
			$this->maxAge = self::NO_OBLIGATION;
141 112
			$this->canPurgeToSaveSpace = true;
142 163
		} elseif ($minValue !== 'auto' && $maxValue === 'auto') {
143
			// Keep for X days but delete anytime if space needed
144 7
			$this->minAge = intval($minValue);
145 7
			$this->maxAge = self::NO_OBLIGATION;
146 7
			$this->canPurgeToSaveSpace = true;
147 51
		} elseif ($minValue === 'auto' && $maxValue !== 'auto') {
148
			// Delete anytime if space needed, Delete all older than max automatically
149 18
			$this->minAge = self::NO_OBLIGATION;
150 18
			$this->maxAge = intval($maxValue);
151 18
			$this->canPurgeToSaveSpace = true;
152 44
		} elseif ($minValue !== 'auto' && $maxValue !== 'auto') {
153
			// Delete all older than max OR older than min if space needed
154
155
			// Max < Min as per https://github.com/owncloud/core/issues/16300
156 26
			if ($maxValue < $minValue) {
157 9
				$maxValue = $minValue;
158 9
			}
159
160 26
			$this->minAge = intval($minValue);
161 26
			$this->maxAge = intval($maxValue);
162 26
			$this->canPurgeToSaveSpace = false;
163 26
		}
164 163
	}
165
}
166