Generate   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 7 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 3
dl 7
loc 100
ccs 54
cts 54
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A configure() 0 23 1
B execute() 0 46 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\Command;
25
26
use OCP\AppFramework\Utility\ITimeFactory;
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use OCP\Notification\IManager;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class Generate extends Command {
37
38
	/** @var ITimeFactory */
39
	protected $timeFactory;
40
41
	/** @var IUserManager */
42
	protected $userManager;
43
44
	/** @var IManager */
45
	protected $notificationManager;
46
47
	/**
48
	 * @param ITimeFactory $timeFactory
49
	 * @param IUserManager $userManager
50
	 * @param IManager $notificationManager
51
	 */
52 7 View Code Duplication
	public function __construct(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...
53 7
		parent::__construct();
54
55 7
		$this->timeFactory = $timeFactory;
56 7
		$this->userManager = $userManager;
57 7
		$this->notificationManager = $notificationManager;
58 7
	}
59
60 7
	protected function configure() {
61
		$this
62 7
			->setName('notification:generate')
63 7
			->setDescription('Generate a notification for the given user')
64 7
			->addArgument(
65 7
				'user-id',
66 7
				InputArgument::REQUIRED,
67 7
				'User ID of the user to notify'
68
			)
69 7
			->addArgument(
70 7
				'short-message',
71 7
				InputArgument::REQUIRED,
72 7
				'Short message to be sent to the user (max. 255 characters)'
73
			)
74 7
			->addOption(
75 7
				'long-message',
76 7
				'l',
77 7
				InputOption::VALUE_REQUIRED,
78 7
				'Long mesage to be sent to the user (max. 4000 characters)',
79 7
				''
80
			)
81
		;
82 7
	}
83
84
	/**
85
	 * @param InputInterface $input
86
	 * @param OutputInterface $output
87
	 * @return int
88
	 */
89 7
	protected function execute(InputInterface $input, OutputInterface $output): int {
90
91 7
		$userId = $input->getArgument('user-id');
92 7
		$subject = $input->getArgument('short-message');
93 7
		$message = $input->getOption('long-message');
94
95 7
		$user = $this->userManager->get($userId);
96 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...
97 1
			$output->writeln('Unknown user');
98 1
			return 1;
99
		}
100
101 6
		if ($subject === '' || strlen($subject) > 255) {
102 2
			$output->writeln('Too long or empty short-message');
103 2
			return 1;
104
		}
105
106 4
		if ($message !== '' && strlen($message) > 4000) {
107 1
			$output->writeln('Too long long-message');
108 1
			return 1;
109
		}
110
111 3
		$notification = $this->notificationManager->createNotification();
112 3
		$time = $this->timeFactory->getTime();
113 3
		$datetime = new \DateTime();
114 3
		$datetime->setTimestamp($time);
115
116
		try {
117 3
			$notification->setApp('admin_notifications')
118 3
				->setUser($user->getUID())
119 3
				->setDateTime($datetime)
120 3
				->setObject('admin_notifications', dechex($time))
121 3
				->setSubject('cli', [$subject]);
122
123 3
			if ($message !== '') {
124 2
				$notification->setMessage('cli', [$message]);
125
			}
126
127 3
			$this->notificationManager->notify($notification);
128 1
		} catch (\InvalidArgumentException $e) {
129 1
			$output->writeln('Error while sending the notification');
130 1
			return 1;
131
		}
132
133 2
		return 0;
134
	}
135
}
136