Completed
Pull Request — master (#1523)
by
unknown
12:04
created

AliasesService::delete()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 9
loc 9
rs 9.6666
cc 2
eloc 7
nc 3
nop 1
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
	 * @return String Alias
56
	 */
57
	public function findAll($accountId) {
58
		return $this->mapper->findAll($accountId);
59
	}
60
61
	/**
62
	 * @param int $accountId
63
	 * @param String $alias_name
64
	 * @return Alias[]
65
	 */
66
	public function create($accountId, $alias_name) {
67
		try {
68
			$alias = new Alias();
69
			$alias->setAccountId($accountId);
70
			$alias->setAlias($alias_name);
71
			return $this->mapper->insert($alias);
72
		} catch(Exception $e){
73
			$this->handleException($e);
74
		}
75
	}
76
77
	/**
78
	 * @param int $id
79
	 * @return \OCA\Mail\Db\Alias[]
80
	 */
81 View Code Duplication
	public function delete($id) {
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...
82
		try {
83
			$alias = $this->mapper->find($id);
84
			$this->mapper->delete($alias);
85
			return $alias;
86
		} catch(Exception $e) {
87
			$this->handleException($e);
88
		}
89
	}
90
91
	/**
92
	 * @param int $id
93
	 * @param string $alias_name
94
	 * @return Alias[]
95
	 */
96 View Code Duplication
	public function update($id, $alias_name) {
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...
97
		try {
98
			$alias = $this->mapper->find($id);
99
			$alias->setAlias($alias_name);
0 ignored issues
show
Bug introduced by
The method setAlias 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...
100
			return $this->mapper->update($alias);
101
		} catch(Exception $e) {
102
			$this->handleException($e);
103
		}
104
105
	}
106
}
107