APIController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 9.86 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 7
loc 71
ccs 29
cts 29
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
B generateNotification() 0 38 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[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\Notifications\Controller;
25
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\OCSController;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\IRequest;
31
use OCP\IUser;
32
use OCP\IUserManager;
33
use OCP\Notification\IManager;
34
35
class APIController extends OCSController {
36
37
	/** @var ITimeFactory */
38
	protected $timeFactory;
39
40
	/** @var IUserManager */
41
	protected $userManager;
42
43
	/** @var IManager */
44
	protected $notificationManager;
45
46
	/**
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param ITimeFactory $timeFactory
50
	 * @param IUserManager $userManager
51
	 * @param IManager $notificationManager
52
	 */
53 7 View Code Duplication
	public function __construct($appName, IRequest $request, ITimeFactory $timeFactory, IUserManager $userManager, IManager $notificationManager) {
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...
54 7
		parent::__construct($appName, $request);
55
56 7
		$this->timeFactory = $timeFactory;
57 7
		$this->userManager = $userManager;
58 7
		$this->notificationManager = $notificationManager;
59 7
	}
60
61
	/**
62
	 * @param string $userId
63
	 * @param string $shortMessage
64
	 * @param string $longMessage
65
	 * @return DataResponse
66
	 */
67 7
	public function generateNotification(string $userId, string $shortMessage, string $longMessage = ''): DataResponse {
68 7
		$user = $this->userManager->get($userId);
69
70 7
		if (!$user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser 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...
71 1
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
72
		}
73
74 6
		if ($shortMessage === '' || strlen($shortMessage) > 255) {
75 2
			return new DataResponse(null, Http::STATUS_BAD_REQUEST);
76
		}
77
78 4
		if ($longMessage !== '' && strlen($longMessage) > 4000) {
79 1
			return new DataResponse(null, Http::STATUS_BAD_REQUEST);
80
		}
81
82 3
		$notification = $this->notificationManager->createNotification();
83 3
		$time = $this->timeFactory->getTime();
84 3
		$datetime = new \DateTime();
85 3
		$datetime->setTimestamp($time);
86
87
		try {
88 3
			$notification->setApp('admin_notifications')
89 3
				->setUser($user->getUID())
90 3
				->setDateTime($datetime)
91 3
				->setObject('admin_notifications', dechex($time))
92 3
				->setSubject('ocs', [$shortMessage]);
93
94 3
			if ($longMessage !== '') {
95 2
				$notification->setMessage('ocs', [$longMessage]);
96
			}
97
98 3
			$this->notificationManager->notify($notification);
99 1
		} catch (\InvalidArgumentException $e) {
100 1
			return new DataResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR);
101
		}
102
103 2
		return new DataResponse();
104
	}
105
}
106