Passed
Pull Request — master (#1187)
by René
13:55 queued 05:55
created

PreferencesService::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 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 OCA\Polls\Exceptions\NotAuthorizedException;
27
28
use OCA\Polls\Db\Preferences;
29
use OCA\Polls\Db\PreferencesMapper;
30
31
class PreferencesService {
32
33
	/** @var PreferencesMapper */
34
	private $preferencesMapper;
35
36
	/** @var PreferencesMapper */
37
	private $preferences;
38
39
	/** @var String */
40
	private $userId;
41
42
	/**
43
	 * SystemService constructor.
44
	 * @param PreferencesMapper $preferencesMapper
45
	 */
46
	public function __construct(
47
		$UserId,
48
		PreferencesMapper $preferencesMapper
49
	) {
50
		$this->userId = $UserId;
51
		$this->preferencesMapper = $preferencesMapper;
52
		try {
53
			$this->preferences = $this->preferencesMapper->find($this->userId);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->preferencesMapper->find($this->userId) of type OCA\Polls\Db\Preferences is incompatible with the declared type OCA\Polls\Db\PreferencesMapper of property $preferences.

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 (\Exception $e) {
55
			$this->preferences = new Preferences();
56
			$preferences->setUserId($this->userId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $preferences seems to be never defined.
Loading history...
57
			$preferences = $this->preferencesMapper->insert($preferences);
0 ignored issues
show
Unused Code introduced by
The assignment to $preferences is dead and can be removed.
Loading history...
58
		}
59
	}
60
	/**
61
	 * get
62
	 * Read all preferences
63
	 * @NoAdminRequired
64
	 * @NoCSRFRequired
65
	 * @return Preferences
66
	 */
67
	public function get() {
68
		return $this->preferences;
69
	}
70
71
	/**
72
	 * write
73
	 * Write references
74
	 * @NoAdminRequired
75
	 * @param array $settings
76
	 * @return Preferences
77
	 * @throws NotAuthorizedException
78
	 */
79
	public function write($settings) {
80
		if (!$this->userId) {
81
			throw new NotAuthorizedException;
82
		}
83
84
		$this->preferences->setPreferences(json_encode($settings));
0 ignored issues
show
Bug introduced by
The method setPreferences() does not exist on OCA\Polls\Db\PreferencesMapper. ( Ignorable by Annotation )

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

84
		$this->preferences->/** @scrutinizer ignore-call */ 
85
                      setPreferences(json_encode($settings));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
		$this->preferences->setTimestamp(time());
0 ignored issues
show
Bug introduced by
The method setTimestamp() does not exist on OCA\Polls\Db\PreferencesMapper. ( Ignorable by Annotation )

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

85
		$this->preferences->/** @scrutinizer ignore-call */ 
86
                      setTimestamp(time());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
		return $this->preferencesMapper->update($this->preferences);
0 ignored issues
show
Bug introduced by
$this->preferences of type OCA\Polls\Db\PreferencesMapper is incompatible with the type OCP\AppFramework\Db\Entity expected by parameter $entity of OCP\AppFramework\Db\QBMapper::update(). ( Ignorable by Annotation )

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

86
		return $this->preferencesMapper->update(/** @scrutinizer ignore-type */ $this->preferences);
Loading history...
87
	}
88
}
89