Completed
Pull Request — master (#362)
by Maxence
02:10 queued 11s
created

MemberCreate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 12.35 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 7
dl 10
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A verify() 0 11 1
A manage() 0 16 3

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 declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\GlobalScale;
31
32
33
use OCA\Circles\Db\CirclesRequest;
34
use OCA\Circles\Db\MembersRequest;
35
use OCA\Circles\Exceptions\CircleDoesNotExistException;
36
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
37
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
38
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
39
use OCA\Circles\Model\GlobalScale\GSEvent;
40
use OCA\Circles\Service\CirclesService;
41
use OCA\Circles\Service\EventsService;
42
use OCA\Circles\Service\MiscService;
43
44
45
/**
46
 * Class MemberCreate
47
 *
48
 * @package OCA\Circles\GlobalScale
49
 */
50
class MemberCreate implements IGlobalScaleEvent {
51
52
53
	/** @var CirclesRequest */
54
	protected $circlesRequest;
55
56
	/** @var MembersRequest */
57
	protected $membersRequest;
58
59
	/** @var CirclesService */
60
	protected $circlesService;
61
62
	/** @var EventsService */
63
	protected $eventsService;
64
65
	/** @var MiscService */
66
	protected $miscService;
67
68
69
	/**
70
	 * MemberCreate constructor.
71
	 *
72
	 * @param CirclesRequest $circlesRequest
73
	 * @param MembersRequest $membersRequest
74
	 * @param CirclesService $circlesService
75
	 * @param EventsService $eventsService
76
	 * @param MiscService $miscService
77
	 */
78 View Code Duplication
	public function __construct(
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...
79
		CirclesRequest $circlesRequest, MembersRequest $membersRequest, CirclesService $circlesService,
80
		EventsService $eventsService, MiscService $miscService
81
	) {
82
		$this->circlesRequest = $circlesRequest;
83
		$this->membersRequest = $membersRequest;
84
		$this->circlesService = $circlesService;
85
		$this->eventsService = $eventsService;
86
		$this->miscService = $miscService;
87
	}
88
89
90
	/**
91
	 * @param GSEvent $event
92
	 *
93
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
94
	 */
95
	public function verify(GSEvent $event): void {
96
		$eventCircle = $event->getCircle();
97
		$viewer = $event->getViewer();
98
		$eventViewer = $viewer->getUserId();
99
		$eventInstance = $viewer->getInstance();
100
101
			$circle =
102
				$this->circlesRequest->getCircle($eventCircle->getUniqueId(), $eventViewer, $eventInstance);
103
			$circle->getHigherViewer()
104
				   ->hasToBeModerator();
105
	}
106
107
108
	/**
109
	 * @param GSEvent $event
110
	 *
111
	 * @throws MemberAlreadyExistsException
112
	 */
113
	public function manage(GSEvent $event): void {
114
		$this->miscService->log('new event; ' . json_encode($event));
115
		if (!$event->hasCircle() || !$event->hasMember()) {
116
			return;
117
		}
118
119
		$member = $event->getMember();
120
		$this->miscService->log('new event; ' . json_encode($member));
121
122
//		if ($member->getInstance() === '') {
123
//			$member->setInstance($event->getSource());
124
//		}
125
126
		$this->membersRequest->createMember($member);
127
		$this->eventsService->onMemberNew($event->getCircle(), $member);
128
	}
129
130
}
131
132