Completed
Push — master ( 99c28a...4c4331 )
by Robin
47:48
created

Expiration   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 138
Duplicated Lines 60.87 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%
Metric Value
wmc 29
lcom 1
cbo 2
dl 84
loc 138
ccs 64
cts 68
cp 0.9412
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A isEnabled() 0 3 1
D isExpired() 36 36 9
A getMaxAgeAsTimestamp() 8 8 3
C parseRetentionObligation() 32 44 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 125 View Code Duplication
	public function __construct(IConfig $config,ITimeFactory $timeFactory){
50 125
		$this->timeFactory = $timeFactory;
51 125
		$this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto');
52
53 125
		if ($this->retentionObligation !== 'disabled') {
54 120
			$this->parseRetentionObligation();
55 120
		}
56 125
	}
57
58
	/**
59
	 * Is trashbin expiration enabled
60
	 * @return bool
61
	 */
62 98
	public function isEnabled(){
63 98
		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 120
	private function parseRetentionObligation(){
122 120
		$splitValues = explode(',', $this->retentionObligation);
123 120 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 120
			$minValue = trim($splitValues[0]);
127
		}
128
129 120
		if (!isset($splitValues[1]) && $minValue === 'auto') {
130 69
			$maxValue = 'auto';
131 120
		} elseif (!isset($splitValues[1])) {
132
			$maxValue = self::DEFAULT_RETENTION_OBLIGATION;
133
		} else {
134 51
			$maxValue = trim($splitValues[1]);
135
		}
136
137 120 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 77
			$this->minAge = self::DEFAULT_RETENTION_OBLIGATION;
140 77
			$this->maxAge = self::NO_OBLIGATION;
141 77
			$this->canPurgeToSaveSpace = true;
142 120
		} 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 43
		} 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 36
		} 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 18
			if ($maxValue < $minValue) {
157 9
				$maxValue = $minValue;
158 9
			}
159
160 18
			$this->minAge = intval($minValue);
161 18
			$this->maxAge = intval($maxValue);
162 18
			$this->canPurgeToSaveSpace = false;
163 18
		}
164 120
	}
165
}
166