Passed
Pull Request — master (#1284)
by René
03:31
created

LogService::isRepetition()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 10
rs 9.6111
ccs 0
cts 5
cp 0
cc 5
nc 6
nop 0
crap 30
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
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\Polls\Service;
25
26
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCA\Polls\Db\Log;
30
use OCA\Polls\Db\LogMapper;
31
32
class LogService {
33
34
	/** @var LogMapper */
35
	private $logMapper;
36
37
	/** @var Log */
38
	private $log;
39
40
	public function __construct(
41
		LogMapper $logMapper,
42
		Log $log
43
	) {
44
		$this->logMapper = $logMapper;
45
		$this->log = $log;
46
	}
47
48
	/**
49
	 * 	 * Log poll activity
50
	 *
51
	 * @param null|string $userId
52
	 */
53
	public function setLog(int $pollId, string $messageId, ?string $userId = null, ?string $message = null): ?Log {
54
		$this->log = new Log();
55
		$this->log->setPollId($pollId);
56
		$this->log->setCreated(time());
57
		$this->log->setMessageId($messageId);
58
		$this->log->setMessage($message);
59
		if ($userId) {
60
			$this->log->setUserId($userId);
61
		} else {
62
			$this->log->setUserId(\OC::$server->getUserSession()->getUser()->getUID());
63
		}
64
65
		try {
66
			return $this->logMapper->insert($this->log);
67
		} catch (UniqueConstraintViolationException $e) {
68
			return null;
69
		}
70
	}
71
}
72