Passed
Push — master ( 17cdcf...aa80aa )
by John
09:42 queued 11s
created

SecurityProvider::parse()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 56
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 43
nc 10
nop 3
dl 0
loc 56
rs 7.6666
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
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Settings\Activity;
29
30
use InvalidArgumentException;
31
use OCP\Activity\IEvent;
32
use OCP\Activity\IManager;
33
use OCP\Activity\IProvider;
34
use OCP\IURLGenerator;
35
use OCP\L10N\IFactory as L10nFactory;
36
37
class SecurityProvider implements IProvider {
38
39
	/** @var L10nFactory */
40
	private $l10n;
41
42
	/** @var IURLGenerator */
43
	private $urlGenerator;
44
45
	/** @var IManager */
46
	private $activityManager;
47
48
	public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) {
49
		$this->urlGenerator = $urlGenerator;
50
		$this->l10n = $l10n;
51
		$this->activityManager = $activityManager;
52
	}
53
54
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
55
		if ($event->getType() !== 'security') {
56
			throw new InvalidArgumentException();
57
		}
58
59
		$l = $this->l10n->get('settings', $language);
60
61
		switch ($event->getSubject()) {
62
			case 'twofactor_success':
63
				$params = $event->getSubjectParameters();
64
				$event->setParsedSubject($l->t('You successfully logged in using two-factor authentication (%1$s)', [
65
							$params['provider'],
66
					]));
67
				if ($this->activityManager->getRequirePNG()) {
68
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
69
				} else {
70
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
71
				}
72
				break;
73
			case 'twofactor_failed':
74
				$params = $event->getSubjectParameters();
75
				$event->setParsedSubject($l->t('A login attempt using two-factor authentication failed (%1$s)', [
76
							$params['provider'],
77
					]));
78
				if ($this->activityManager->getRequirePNG()) {
79
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
80
				} else {
81
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
82
				}
83
				break;
84
			case 'remote_wipe_start':
85
				$params = $event->getSubjectParameters();
86
				$event->setParsedSubject($l->t('Remote wipe was started on %1$s', [
87
					$params['name'],
88
				]));
89
				if ($this->activityManager->getRequirePNG()) {
90
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
91
				} else {
92
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
93
				}
94
				break;
95
			case 'remote_wipe_finish':
96
				$params = $event->getSubjectParameters();
97
				$event->setParsedSubject($l->t('Remote wipe has finished on %1$s', [
98
					$params['name'],
99
				]));
100
				if ($this->activityManager->getRequirePNG()) {
101
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
102
				} else {
103
					$event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
104
				}
105
				break;
106
			default:
107
				throw new InvalidArgumentException();
108
		}
109
		return $event;
110
	}
111
112
}
113