Completed
Push — master ( d62417...7b2aa5 )
by René
10:14 queued 11s
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 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
nc 6
nop 0
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 30
rs 9.6111
c 1
b 0
f 0
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 Exception;
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
	private $mapper;
35
	private $logItem;
36
37
	/**
38
	 * LogService constructor.
39
	 * @param LogMapper $mapper
40
	 * @param Log $logItem
41
	 */
42
43
	public function __construct(
44
		LogMapper $mapper,
45
		Log $logItem
46
	) {
47
		$this->mapper = $mapper;
48
		$this->logItem = $logItem;
49
	}
50
51
52
	/**
53
	* Prevent repetition of the same log event
54
	* @NoAdminRequired
55
	* @return Bool
56
	*/
57
	public function isRepetition() {
58
		try {
59
			$lastRecord = $this->mapper->getLastRecord($this->logItem->getPollId());
60
			return ( intval($lastRecord->getPollId()) === intval($this->logItem->getPollId())
61
				&& $lastRecord->getUserId() === $this->logItem->getUserId()
62
				&& $lastRecord->getMessageId() === $this->logItem->getMessageId()
63
				&& $lastRecord->getMessage() === $this->logItem->getMessage()
64
			);
65
		} catch (DoesNotExistException $e) {
66
			return false;
67
		}
68
	}
69
70
	/**
71
	* Log poll activity
72
	* @NoAdminRequired
73
	* @param $pollId related pollId
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\related was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
	* @param $messageId identifier for message notice
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\identifier was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
	* @param $userId (optional)
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\optional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
	* @param $message message text if $messageId is === 'custom'
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
	* @return Log
78
	*/
79
	public function setLog($pollId, $messageId, $userId = null, $message = null) {
80
		$this->logItem = new Log();
81
		$this->logItem->setPollId($pollId);
82
		$this->logItem->setCreated(time());
83
		$this->logItem->setMessageId($messageId);
84
		$this->logItem->setMessage($message);
85
86
		if ($userId) {
87
			$this->logItem->setUserId($userId);
88
		} else {
89
			$this->logItem->setUserId(\OC::$server->getUserSession()->getUser()->getUID());
90
		}
91
92
93
		if ($this->isRepetition()) {
94
			return null;
95
		} else {
96
			return $this->mapper->insert($this->logItem);
97
		}
98
	}
99
100
}
101