Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

CronService::expireCredentials()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
nc 3
nop 0
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Service;
25
26
use OCA\Passman\Activity;
27
use OCA\Passman\Utility\Utils;
28
use OCP\DB\Exception;
29
use OCP\DB\QueryBuilder\IQueryBuilder;
30
use OCP\IDBConnection;
31
use Psr\Log\LoggerInterface;
32
33
class CronService {
34
35
	private CredentialService $credentialService;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
36
	private LoggerInterface $logger;
37
	private Utils $utils;
38
	private NotificationService $notificationService;
39
	private ActivityService $activityService;
40
	private IDBConnection $db;
41
42
	public function __construct(CredentialService $credentialService, LoggerInterface $logger, Utils $utils, NotificationService $notificationService, ActivityService $activityService, IDBConnection $db) {
43
		$this->credentialService = $credentialService;
44
		$this->logger = $logger;
45
		$this->utils = $utils;
46
		$this->notificationService = $notificationService;
47
		$this->activityService = $activityService;
48
		$this->db = $db;
49
	}
50
51
	public function expireCredentials() {
52
		$expired_credentials = $this->credentialService->getExpiredCredentials($this->utils->getTime());
53
		foreach ($expired_credentials as $credential) {
54
			$link = ''; // @TODO create direct link to credential
55
			$qb = $this->db->getQueryBuilder();
56
			$qb->select('*')
57
				->from('notifications')
58
				->where($qb->expr()->eq('object_id', $qb->createNamedParameter($credential->getId(), IQueryBuilder::PARAM_INT)))
59
				->andWhere($qb->expr()->eq('subject', $qb->createNamedParameter('credential_expired', IQueryBuilder::PARAM_STR)));
60
61
			try {
62
				$this->logger->debug($credential->getLabel() . ' is expired, checking notifications!', array('app' => 'passman'));
63
				$notificationCount = $qb->execute()->rowCount();
64
				if ($notificationCount === 0) {
65
					$this->logger->debug($credential->getLabel() . ' is expired, adding notification!', array('app' => 'passman'));
66
					$this->activityService->add(
67
						Activity::SUBJECT_ITEM_EXPIRED, array($credential->getLabel(), $credential->getUserId()),
68
						'', array(),
69
						$link, $credential->getUserId(), Activity::TYPE_ITEM_EXPIRED);
70
					$this->notificationService->credentialExpiredNotification($credential);
71
				}
72
			} catch (Exception $exception) {
73
				$this->logger->error('Error while creating a notification: ' . $exception->getMessage(), array('app' => 'passman'));
74
			}
75
		}
76
	}
77
}
78