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

ExampleMembershipsRemoved::handle()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
rs 9.1128
c 0
b 0
f 0
cc 5
nc 3
nop 1
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\MembershipsRemovedEvent;
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 ExampleMembershipsRemoved
50
 *
51
 * @package OCA\Circles\Listeners\Files
52
 */
53 View Code Duplication
class ExampleMembershipsRemoved 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
	 * ExampleMembershipsRemoved 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 MembershipsRemovedEvent) {
89
			return;
90
		}
91
92
		if ($this->configService->getAppValue(ConfigService::EVENT_EXAMPLES) !== '1') {
93
			return;
94
		}
95
96
		$prefix = '[Example Event] (ExampleMembershipsRemoved) ';
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 not ' . $inheritance . ' of '
108
						   . $membership->getCircleId() . ' anymore';
109
				}
110
111
				return $federatedUser->getUserId() . ' (' . Member::$TYPE[$federatedUser->getUserType()]
112
					   . ') is not ' . $inheritance . ' of ' . $membership->getCircleId() . ' anymore';
113
			}, $event->getMemberships()
114
		);
115
116
		$this->log(3, $prefix . implode('. ', $memberships));
117
	}
118
119
}
120
121