Passed
Pull Request — master (#1367)
by René
09:22 queued 04:07
created

WatchService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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 OCP\AppFramework\Db\DoesNotExistException;
27
28
use OCA\Polls\Db\Watch;
29
use OCA\Polls\Db\WatchMapper;
30
31
class WatchService {
32
33
	/** @var WatchMapper */
34
	private $watchMapper;
35
36
	/** @var Watch */
37
	private $watch;
38
39
	/** @var String */
40
	private $userId;
0 ignored issues
show
introduced by
The private property $userId is not used, and could be removed.
Loading history...
41
42
	public function __construct(
43
		WatchMapper $watchMapper
44
	) {
45
		$this->watchMapper = $watchMapper;
46
	}
47
48
	/**
49
	 * @return Watch[]
50
	 */
51
	public function listForPollId(int $pollId): array {
52
		try {
53
			$this->watch = $this->watchMapper->findForPollId($pollId);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->watchMapper->findForPollId($pollId) of type OCP\AppFramework\Db\Entity[] or array is incompatible with the declared type OCA\Polls\Db\Watch of property $watch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
		} catch (DoesNotExistException $e) {
55
			return [];
56
		}
57
	}
58
59
	/**
60
	 * @return Watch[]
61
	 */
62
	public function getUpdates(int $pollId, int $offset): array {
63
		try {
64
			return $this->watchMapper->findUpdatesForPollId($pollId, $offset);
65
		} catch (DoesNotExistException $e) {
66
			return [];
67
		}
68
	}
69
70
	/**
71
	 * @return Watch[]
72
	 */
73
	public function listForTable(string $table): array {
74
		try {
75
			$this->watch = $this->watchMapper->findForTable($table);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->watchMapper->findForTable($table) of type OCP\AppFramework\Db\Entity[] or array is incompatible with the declared type OCA\Polls\Db\Watch of property $watch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
$table of type string is incompatible with the type integer expected by parameter $table of OCA\Polls\Db\WatchMapper::findForTable(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
			$this->watch = $this->watchMapper->findForTable(/** @scrutinizer ignore-type */ $table);
Loading history...
76
		} catch (DoesNotExistException $e) {
77
			return [];
78
		}
79
	}
80
81
	/**
82
	 * @return Watch
83
	 */
84
	public function writeUpdate(int $pollId, string $table): Watch {
85
		try {
86
			$this->watch = $this->watchMapper->findForPollIdAndTable($pollId, $table);
87
		} catch (DoesNotExistException $e) {
88
			$this->watch = new Watch();
89
			$this->watch->setPollId($pollId);
90
			$this->watch->setTable($table);
91
			$this->watch = $this->watchMapper->insert($this->watch);
92
		}
93
94
		$this->watch->setUpdated(time());
95
		return $this->watchMapper->update($this->watch);
96
	}
97
}
98