Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

ExpireTrash::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Victor Dubiniuk <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Files_Trashbin\BackgroundJob;
26
27
use OCP\IConfig;
28
use OCP\IUser;
29
use OCP\IUserManager;
30
use OCA\Files_Trashbin\AppInfo\Application;
31
use OCA\Files_Trashbin\Expiration;
32
use OCA\Files_Trashbin\Helper;
33
use OCA\Files_Trashbin\Trashbin;
34
35
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
36
37
	/**
38
	 * @var Expiration
39
	 */
40
	private $expiration;
41
	
42
	/**
43
	 * @var IUserManager
44
	 */
45
	private $userManager;
46
47
	/**
48
	 * @param IUserManager|null $userManager
49
	 * @param Expiration|null $expiration
50
	 */
51 View Code Duplication
	public function __construct(IUserManager $userManager = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
								Expiration $expiration = null) {
53
		// Run once per 30 minutes
54
		$this->setInterval(60 * 30);
55
56
		if (is_null($expiration) || is_null($userManager)) {
57
			$this->fixDIForJobs();
58
		} else {
59
			$this->userManager = $userManager;
60
			$this->expiration = $expiration;
61
		}
62
	}
63
64
	protected function fixDIForJobs() {
65
		$application = new Application();
66
		$this->userManager = \OC::$server->getUserManager();
67
		$this->expiration = $application->getContainer()->query('Expiration');
68
	}
69
70
	/**
71
	 * @param $argument
72
	 * @throws \Exception
73
	 */
74
	protected function run($argument) {
75
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
76
		if (!$maxAge) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxAge of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
77
			return;
78
		}
79
80
		$this->userManager->callForAllUsers(function(IUser $user) {
81
			$uid = $user->getUID();
82
			if (!$this->setupFS($uid)) {
83
				return;
84
			}
85
			$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
86
			Trashbin::deleteExpiredFiles($dirContent, $uid);
87
		});
88
		
89
		\OC_Util::tearDownFS();
90
	}
91
92
	/**
93
	 * Act on behalf on trash item owner
94
	 * @param string $user
95
	 * @return boolean
96
	 */
97 View Code Duplication
	protected function setupFS($user) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
98
		\OC_Util::tearDownFS();
99
		\OC_Util::setupFS($user);
100
101
		// Check if this user has a trashbin directory
102
		$view = new \OC\Files\View('/' . $user);
103
		if (!$view->is_dir('/files_trashbin/files')) {
104
			return false;
105
		}
106
107
		return true;
108
	}
109
}
110