Completed
Push — master ( b05bdf...ffccbd )
by
unknown
20:10 queued 08:50
created

Notifier::prepare()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 60

Duplication

Lines 36
Ratio 60 %

Importance

Changes 0
Metric Value
cc 10
nc 18
nop 2
dl 36
loc 60
rs 7.006
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2018, ownCloud GmbH
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\FederatedFileSharing;
24
25
use OCP\Notification\INotification;
26
use OCP\Notification\INotifier;
27
28
class Notifier implements INotifier {
29
	/** @var \OCP\L10N\IFactory */
30
	protected $factory;
31
32
	/**
33
	 * @param \OCP\L10N\IFactory $factory
34
	 */
35
	public function __construct(\OCP\L10N\IFactory $factory) {
36
		$this->factory = $factory;
37
	}
38
39
	/**
40
	 * @param INotification $notification
41
	 * @param string $languageCode The code of the language that should be used to prepare the notification
42
	 * @return INotification
43
	 */
44
	public function prepare(INotification $notification, $languageCode) {
45
		if ($notification->getApp() !== 'files_sharing') {
46
			// Not my app => throw
47
			throw new \InvalidArgumentException();
48
		}
49
50
		// Read the language from the notification
51
		$l = $this->factory->get('files_sharing', $languageCode);
52
53
		switch ($notification->getObjectType()) {
54
			// Deal with known subjects
55
			case 'remote_share':
56
				$params = $notification->getSubjectParameters();
57 View Code Duplication
				if ($params[0] !== $params[1] && $params[1] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
					$notification->setParsedSubject(
59
						(string) $l->t('"%1$s" shared "%3$s" with you (on behalf of "%2$s")', $params)
60
					);
61
				} else {
62
					$notification->setParsedSubject(
63
						(string) $l->t('"%1$s" shared "%3$s" with you', $params)
64
					);
65
				}
66
67
				$messageParams = $notification->getMessageParameters();
68 View Code Duplication
				if ($messageParams[0] !== $messageParams[1] && $messageParams[1] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
					$notification->setParsedMessage(
70
						(string) $l->t('"%1$s" invited you to view "%3$s" (on behalf of "%2$s")', $messageParams)
71
					);
72
				} else {
73
					$notification->setParsedMessage(
74
						(string) $l->t('"%1$s" invited you to view "%3$s"', $messageParams)
75
					);
76
				}
77
78
				// Deal with the actions for a known subject
79 View Code Duplication
				foreach ($notification->getActions() as $action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
					switch ($action->getLabel()) {
81
						case 'accept':
82
							$action->setParsedLabel(
83
								(string) $l->t('Accept')
84
							)
85
							->setPrimary(true);
86
							break;
87
88
						case 'decline':
89
							$action->setParsedLabel(
90
								(string) $l->t('Decline')
91
							);
92
							break;
93
					}
94
95
					$notification->addParsedAction($action);
96
				}
97
				return $notification;
98
99
			default:
100
				// Unknown subject => Unknown notification => throw
101
				throw new \InvalidArgumentException();
102
		}
103
	}
104
}
105