Completed
Push — master ( 716c72...0a3015 )
by Christoph
09:06
created

AliasesService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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