GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f7fe9b...f79482 )
by Roeland
22:04 queued 20:02
created

RetentionJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 13
nc 1
nop 6
1
<?php
2
/**
3
 *
4
 * @author Roeland Jago Douma <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
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
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Files_Retention\BackgroundJob;
23
24
use OC\BackgroundJob\TimedJob;
25
use OCA\Files_Retention\Constants;
26
use OCP\AppFramework\Utility\ITimeFactory;
27
use OCP\Files\Config\IUserMountCache;
28
use OCP\IDBConnection;
29
use OCP\Files\Node;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\NotPermittedException;
32
use OCP\Files\IRootFolder;
33
use OCP\SystemTag\ISystemTagManager;
34
use OCP\SystemTag\ISystemTagObjectMapper;
35
36
class RetentionJob extends TimedJob {
37
	/** @var ISystemTagManager */
38
	private $tagManager;
39
40
	/** @var ISystemTagObjectMapper */
41
	private $tagMapper;
42
43
	/** @var IUserMountCache */
44
	private $userMountCache;
45
46
	/** @var IDBConnection */
47
	private $db;
48
49
	/** @var IRootFolder */
50
	private $rootFolder;
51
52
	/**@var ITimeFactory */
53
	private $timeFactory;
54
55
	/**
56
	 * RetentionJob constructor.
57
	 *
58
	 * @param ISystemTagManager $tagManager
59
	 * @param ISystemTagObjectMapper $tagMapper
60
	 * @param IUserMountCache $userMountCache
61
	 * @param IDBConnection $db
62
	 * @param IRootFolder $rootFolder
63
	 * @param ITimeFactory $timeFactory
64
	 */
65
	public function __construct(ISystemTagManager $tagManager,
66
								ISystemTagObjectMapper $tagMapper,
67
								IUserMountCache $userMountCache,
68
								IDBConnection $db,
69
								IRootFolder $rootFolder,
70
								ITimeFactory $timeFactory) {
71
		// Run once a day
72
		$this->setInterval(24 * 60 * 60);
73
74
		$this->tagManager = $tagManager;
75
		$this->tagMapper = $tagMapper;
76
		$this->userMountCache = $userMountCache;
77
		$this->db = $db;
78
		$this->rootFolder = $rootFolder;
79
		$this->timeFactory = $timeFactory;
80
	}
81
82
	public function run($argument) {
83
		// Validate if tag still exists
84
		$tag = $argument['tag'];
85
		try {
86
			$this->tagManager->getTagsByIds($tag);
87
		} catch (\InvalidArgumentException $e) {
88
			// tag no longer exists error out
89
			return;
90
		}
91
92
		// Validate if there is an entry in the DB
93
		$qb = $this->db->getQueryBuilder();
94
		$qb->select('*')
95
			->from('retention')
96
			->where($qb->expr()->eq('tag_id', $qb->createNamedParameter($tag)));
97
98
		$cursor = $qb->execute();
99
		$data = $cursor->fetch();
100
		$cursor->closeCursor();
101
102
		if ($data === false) {
103
			// No entry anymore in the retention db
104
			return;
105
		}
106
107
		// Calculate before date only once
108
		$deleteBefore = $this->getBeforeDate((int)$data['time_unit'], (int)$data['time_amount']);
109
110
		$offset = 0;
111
		$limit = 1000;
112
		while(true) {
113
			$fileids = $this->tagMapper->getObjectIdsForTags($tag, 'files', $limit, $offset);
114
115
			foreach ($fileids as $fileid) {
116
				try {
117
					$node = $this->checkFileId($fileid);
118
				} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
					continue;
120
				}
121
122
				$this->expireNode($node, $deleteBefore);
123
			}
124
125
			if (empty($fileids) || count($fileids) < $limit) {
126
				break;
127
			}
128
129
			$offset += $limit;
130
		}
131
	}
132
133
	/**
134
	 * Get a node for the given fileid.
135
	 *
136
	 * @param int $fileid
137
	 * @return Node
138
	 * @throws NotFoundException
139
	 */
140
	private function checkFileId($fileid) {
141
		$mountPoints = $this->userMountCache->getMountsForFileId($fileid);
142
143
		if (count($mountPoints) === 0) {
144
			throw new NotFoundException();
145
		}
146
147
		$mountPoint = $mountPoints[0];
148
149
		$userFolder = $this->rootFolder->getUserFolder($mountPoint->getUser()->getUID());
150
151
		$nodes = $userFolder->getById($fileid);
152
		if (count($nodes) === 0) {
153
			throw new NotFoundException();
154
		}
155
156
		return $nodes[0];
157
	}
158
159
	/**
160
	 * @param Node $node
161
	 * @param \DateTime $deleteBefore
162
	 */
163
	private function expireNode(Node $node, \DateTime $deleteBefore) {
164
		$mtime = new \DateTime();
165
		$mtime->setTimestamp($node->getMTime());
166
167
		if ($mtime < $deleteBefore) {
168
			try {
169
				$node->delete();
170
			} catch (NotPermittedException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotPermittedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
171
				//LOG?
172
			}
173
		}
174
175
	}
176
177
	/**
178
	 * @param int $timeunit
179
	 * @param int $timeamount
180
	 * @return \DateTime
181
	 */
182
	private function getBeforeDate($timeunit, $timeamount) {
183
		$spec = 'P' . $timeamount;
184
185
		if ($timeunit === Constants::DAY) {
186
			$spec .= 'D';
187
		} else if ($timeunit === Constants::WEEK) {
188
			$spec .= 'W';
189
		} else if ($timeunit === Constants::MONTH) {
190
			$spec .= 'M';
191
		} else if ($timeunit === Constants::YEAR) {
192
			$spec .= 'Y';
193
		}
194
195
		$delta = new \DateInterval($spec);
196
		$currentDate = new \DateTime();
197
		$currentDate->setTimestamp($this->timeFactory->getTime());
198
199
		return $currentDate->sub($delta);
200
	}
201
}