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.

RetentionJob::getBeforeDate()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 8.8571
cc 5
eloc 14
nc 5
nop 2
crap 5
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\BackgroundJob\IJobList;
28
use OCP\Files\Config\IUserMountCache;
29
use OCP\IDBConnection;
30
use OCP\Files\Node;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\NotPermittedException;
33
use OCP\Files\IRootFolder;
34
use OCP\SystemTag\ISystemTagManager;
35
use OCP\SystemTag\ISystemTagObjectMapper;
36
37
class RetentionJob extends TimedJob {
38
	/** @var ISystemTagManager */
39
	private $tagManager;
40
41
	/** @var ISystemTagObjectMapper */
42
	private $tagMapper;
43
44
	/** @var IUserMountCache */
45
	private $userMountCache;
46
47
	/** @var IDBConnection */
48
	private $db;
49
50
	/** @var IRootFolder */
51
	private $rootFolder;
52
53
	/** @var ITimeFactory */
54
	private $timeFactory;
55
56
	/** @var IJobList */
57
	private $jobList;
58
59
	/**
60
	 * RetentionJob constructor.
61
	 *
62
	 * @param ISystemTagManager $tagManager
63
	 * @param ISystemTagObjectMapper $tagMapper
64
	 * @param IUserMountCache $userMountCache
65
	 * @param IDBConnection $db
66
	 * @param IRootFolder $rootFolder
67
	 * @param ITimeFactory $timeFactory
68
	 * @param IJobList $jobList
69
	 */
70 156
	public function __construct(ISystemTagManager $tagManager,
71
								ISystemTagObjectMapper $tagMapper,
72
								IUserMountCache $userMountCache,
73
								IDBConnection $db,
74
								IRootFolder $rootFolder,
75
								ITimeFactory $timeFactory,
76
								IJobList $jobList) {
77
		// Run once a day
78 156
		$this->setInterval(24 * 60 * 60);
79
80 156
		$this->tagManager = $tagManager;
81 156
		$this->tagMapper = $tagMapper;
82 156
		$this->userMountCache = $userMountCache;
83 156
		$this->db = $db;
84 156
		$this->rootFolder = $rootFolder;
85 156
		$this->timeFactory = $timeFactory;
86 156
		$this->jobList = $jobList;
87 156
	}
88
89 156
	public function run($argument) {
90
		// Validate if tag still exists
91 156
		$tag = $argument['tag'];
92
		try {
93 156
			$this->tagManager->getTagsByIds($tag);
94 131
		} catch (\InvalidArgumentException $e) {
95
			// tag no longer exists remove backgroundjob and exit
96 6
			$this->jobList->remove($this, $argument);
97 6
			return;
98
		}
99
100
		// Validate if there is an entry in the DB
101 150
		$qb = $this->db->getQueryBuilder();
102 150
		$qb->select('*')
103 150
			->from('retention')
104 150
			->where($qb->expr()->eq('tag_id', $qb->createNamedParameter($tag)));
105
106 150
		$cursor = $qb->execute();
107 150
		$data = $cursor->fetch();
108 150
		$cursor->closeCursor();
109
110 150
		if ($data === false) {
111
			// No entry anymore in the retention db
112 6
			$this->jobList->remove($this, $argument);
113 6
			return;
114
		}
115
116
		// Calculate before date only once
117 144
		$deleteBefore = $this->getBeforeDate((int)$data['time_unit'], (int)$data['time_amount']);
118
119 144
		$offset = 0;
120 144
		$limit = 1000;
121 144
		while(true) {
122 144
			$fileids = $this->tagMapper->getObjectIdsForTags($tag, 'files', $limit, $offset);
123
124 144
			foreach ($fileids as $fileid) {
125
				try {
126 144
					$node = $this->checkFileId($fileid);
127 123
				} 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...
128 18
					continue;
129
				}
130
131 126
				$this->expireNode($node, $deleteBefore);
132 120
			}
133
134 144
			if (empty($fileids) || count($fileids) < $limit) {
135 144
				break;
136
			}
137
138 6
			$offset += $limit;
139 5
		}
140 144
	}
141
142
	/**
143
	 * Get a node for the given fileid.
144
	 *
145
	 * @param int $fileid
146
	 * @return Node
147
	 * @throws NotFoundException
148
	 */
149 144
	private function checkFileId($fileid) {
150 144
		$mountPoints = $this->userMountCache->getMountsForFileId($fileid);
151
152 144
		if (count($mountPoints) === 0) {
153 12
			throw new NotFoundException();
154
		}
155
156 132
		$mountPoint = $mountPoints[0];
157
158 132
		$userFolder = $this->rootFolder->getUserFolder($mountPoint->getUser()->getUID());
159
160 132
		$nodes = $userFolder->getById($fileid);
161 132
		if (count($nodes) === 0) {
162 6
			throw new NotFoundException();
163
		}
164
165 126
		return $nodes[0];
166
	}
167
168
	/**
169
	 * @param Node $node
170
	 * @param \DateTime $deleteBefore
171
	 */
172 126
	private function expireNode(Node $node, \DateTime $deleteBefore) {
173 126
		$mtime = new \DateTime();
174 126
		$mtime->setTimestamp($node->getMTime());
175
176 126
		if ($mtime < $deleteBefore) {
177
			try {
178 66
				$node->delete();
179 56
			} 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...
180
				//LOG?
181
			}
182 55
		}
183
184 126
	}
185
186
	/**
187
	 * @param int $timeunit
188
	 * @param int $timeamount
189
	 * @return \DateTime
190
	 */
191 144
	private function getBeforeDate($timeunit, $timeamount) {
192 144
		$spec = 'P' . $timeamount;
193
194 144
		if ($timeunit === Constants::DAY) {
195 30
			$spec .= 'D';
196 139
		} else if ($timeunit === Constants::WEEK) {
197 54
			$spec .= 'W';
198 105
		} else if ($timeunit === Constants::MONTH) {
199 30
			$spec .= 'M';
200 55
		} else if ($timeunit === Constants::YEAR) {
201 30
			$spec .= 'Y';
202 25
		}
203
204 144
		$delta = new \DateInterval($spec);
205 144
		$currentDate = new \DateTime();
206 144
		$currentDate->setTimestamp($this->timeFactory->getTime());
207
208 144
		return $currentDate->sub($delta);
209
	}
210
}
211