Completed
Pull Request — master (#1616)
by
unknown
08:27
created

AliasesService::updateSignature()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 2
eloc 7
nc 4
nop 3
crap 6
1
<?php
2
/**
3
 * @author Tahaa Karim <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
namespace OCA\Mail\Service;
21
22
use Exception;
23
use OCA\Mail\Db\Alias;
24
use OCA\Mail\Db\AliasMapper;
25
use OCP\AppFramework\Db\DoesNotExistException;
26
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
27
28
class AliasesService {
29
30
	/** @var \OCA\Mail\Db\AliasMapper */
31
	private $mapper;
32
33
	/**
34
	 * @param AliasMapper $mapper
35
	 */
36
	public function __construct(AliasMapper $mapper) {
37
		$this->mapper = $mapper;
38
	}
39
40
	/**
41
	 * @param $e
42
	 * @throws NotFoundException
43
	 */
44
	private function handleException ($e) {
45
		if ($e instanceof DoesNotExistException ||
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
			$e instanceof MultipleObjectsReturnedException) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\Mult...bjectsReturnedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
			throw new NotFoundException($e->getMessage());
48
		} else {
49
			throw $e;
50
		}
51
	}
52
53
	/**
54
	 * @param int $accountId
55
	 * @param String $currentUserId
56
	 * @return Alias[]
57
	 */
58
	public function findAll($accountId, $currentUserId) {
59
		return $this->mapper->findAll($accountId, $currentUserId);
60
	}
61
62
	/**
63
	 * @param int $aliasId
64
	 * @param String $currentUserId
65
	 * @return Alias
66
	 */
67
	public function find($aliasId, $currentUserId) {
68
		return $this->mapper->find($aliasId, $currentUserId);
69
	}
70
71
	/**
72
	 * @param int $accountId
73
	 * @param String $alias
74
	 * @param String $aliasName
75
	 * @return Alias
76
	 */
77
	public function create($accountId, $alias, $aliasName) {
78
		try {
79
			$aliasEntity = new Alias();
80
			$aliasEntity->setAccountId($accountId);
81
			$aliasEntity->setAlias($alias);
82
			$aliasEntity->setName($aliasName);
83
			return $this->mapper->insert($aliasEntity);
84
		} catch(Exception $e){
85
			$this->handleException($e);
86
		}
87
	}
88
89
	/**
90
	 * @param int $aliasId
91
	 * @param String $currentUserId
92
	 * @return \OCA\Mail\Db\Alias
93
	 */
94
	public function delete($aliasId, $currentUserId) {
95
		try {
96
			$alias = $this->mapper->find($aliasId, $currentUserId);
97
			$this->mapper->delete($alias);
98
			return $alias;
99
		} catch(Exception $e) {
100
			$this->handleException($e);
101
		}
102
	}
103
104
	/**
105
	 * @param int $aliasId
106
	 * @param String $currentUserId
107
	 * @param String $signature
108
	 * @return \OCA\Mail\Db\Alias
109
	 */
110 View Code Duplication
	public function updateSignature($aliasId, $currentUserId, $signature) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
		try {
112
			$alias = $this->mapper->find($aliasId, $currentUserId);
113
			$alias->setSignature($signature);
0 ignored issues
show
Bug introduced by
The method setSignature cannot be called on $alias (of type array<integer,object<OCA\Mail\Db\Alias>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114
			return $this->mapper->update($alias);
115
		} catch(Exception $e) {
116
			$this->handleException($e);
117
		}
118
	}
119
}
120