Completed
Push — master ( 21cafd...f8797c )
by Maxence
02:23
created

ExampleMembershipsCreated   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 67
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A handle() 31 31 5

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
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Listeners\Examples;
33
34
35
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
36
use daita\MySmallPhpTools\Traits\TStringTools;
37
use Exception;
38
use OCA\Circles\AppInfo\Application;
39
use OCA\Circles\CirclesManager;
40
use OCA\Circles\Events\MembershipsCreatedEvent;
41
use OCA\Circles\Model\Member;
42
use OCA\Circles\Model\Membership;
43
use OCA\Circles\Service\ConfigService;
44
use OCP\EventDispatcher\Event;
45
use OCP\EventDispatcher\IEventListener;
46
47
48
/**
49
 * Class ExampleMembershipsCreated
50
 *
51
 * @package OCA\Circles\Listeners\Files
52
 */
53 View Code Duplication
class ExampleMembershipsCreated implements IEventListener {
0 ignored issues
show
Duplication introduced by
This class 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
55
56
	use TStringTools;
57
	use TNC22Logger;
58
59
60
	/** @var CirclesManager */
61
	private $circlesManager;
62
63
	/** @var ConfigService */
64
	private $configService;
65
66
67
	/**
68
	 * ExampleCircleMemberAdded constructor.
69
	 *
70
	 * @param CirclesManager $circlesManager
71
	 * @param ConfigService $configService
72
	 */
73
	public function __construct(
74
		CirclesManager $circlesManager,
75
		ConfigService $configService
76
	) {
77
		$this->circlesManager = $circlesManager;
78
		$this->configService = $configService;
79
80
		$this->setup('app', Application::APP_ID);
81
	}
82
83
84
	/**
85
	 * @param Event $event
86
	 */
87
	public function handle(Event $event): void {
88
		if (!$event instanceof MembershipsCreatedEvent) {
89
			return;
90
		}
91
92
		if ($this->configService->getAppValue(ConfigService::EVENT_EXAMPLES) !== '1') {
93
			return;
94
		}
95
96
		$prefix = '[Example Event] (ExampleMembershipsCreated) ';
97
98
		$memberships = array_map(
99
			function(Membership $membership) {
100
				$inheritance = ($membership->getInheritanceDepth() > 1) ?
101
					'an inherited member' : 'a direct member';
102
				try {
103
					$federatedUser = $this->circlesManager->getFederatedUser($membership->getSingleId());
104
				} catch (Exception $e) {
105
					$this->e($e);
106
107
					return $membership->getSingleId() . ' is now ' . $inheritance . ' of '
108
						   . $membership->getCircleId();
109
				}
110
111
				return $federatedUser->getUserId() . ' (' . Member::$TYPE[$federatedUser->getUserType()]
112
					   . ') is now ' . $inheritance . ' of ' . $membership->getCircleId();
113
			}, $event->getMemberships()
114
		);
115
116
		$this->log(3, $prefix . implode('. ', $memberships));
117
	}
118
119
}
120
121