Completed
Pull Request — master (#362)
by Maxence
01:44
created

MemberJoin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 9
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A verify() 0 13 1
A manage() 0 12 2

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\MemberAlreadyExistsException;
36
use OCA\Circles\Exceptions\MemberCantJoinCircleException;
37
use OCA\Circles\Exceptions\MemberIsBlockedException;
38
use OCA\Circles\Exceptions\MembersLimitException;
39
use OCA\Circles\Model\GlobalScale\GSEvent;
40
use OCA\Circles\Model\Member;
41
use OCA\Circles\Service\CirclesService;
42
use OCA\Circles\Service\EventsService;
43
use OCA\Circles\Service\MiscService;
44
45
46
/**
47
 * Class MemberJoin
48
 *
49
 * @package OCA\Circles\GlobalScale
50
 */
51
class MemberJoin implements IGlobalScaleEvent {
52
53
54
	/** @var MembersRequest */
55
	protected $membersRequest;
56
57
	/** @var CirclesService */
58
	protected $circlesService;
59
60
	/** @var EventsService */
61
	protected $eventsService;
62
63
	/** @var MiscService */
64
	protected $miscService;
65
66
67
	/**
68
	 * MemberCreate constructor.
69
	 *
70
	 * @param CirclesRequest $circlesRequest
71
	 * @param MembersRequest $membersRequest
72
	 * @param CirclesService $circlesService
73
	 * @param EventsService $eventsService
74
	 * @param MiscService $miscService
75
	 */
76 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...
77
		CirclesRequest $circlesRequest, MembersRequest $membersRequest, CirclesService $circlesService,
0 ignored issues
show
Unused Code introduced by
The parameter $circlesRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
		EventsService $eventsService, MiscService $miscService
79
	) {
80
		$this->membersRequest = $membersRequest;
81
		$this->circlesService = $circlesService;
82
		$this->eventsService = $eventsService;
83
		$this->miscService = $miscService;
84
	}
85
86
87
	/**
88
	 * @param GSEvent $event
89
	 *
90
	 * @throws MemberAlreadyExistsException
91
	 * @throws MemberIsBlockedException
92
	 * @throws MembersLimitException
93
	 * @throws MemberCantJoinCircleException
94
	 */
95
	public function verify(GSEvent $event): void {
96
		$circle = $event->getCircle();
97
		$eventMember = $event->getMember();
98
99
		$member = $this->membersRequest->getFreshNewMember(
100
			$circle->getUniqueId(), $eventMember->getUserId(), Member::TYPE_USER, $eventMember->getInstance()
101
		);
102
		$member->hasToBeAbleToJoinTheCircle();
103
		$member->joinCircle($circle->getType());
104
105
		$this->circlesService->checkThatCircleIsNotFull($circle);
106
		$event->setMember($member);
107
	}
108
109
110
	/**
111
	 * @param GSEvent $event
112
	 *
113
	 * @return bool|void
114
	 * @throws MemberAlreadyExistsException
115
	 */
116
	public function manage(GSEvent $event): void {
117
		$circle = $event->getCircle();
118
		$member = $event->getMember();
119
120
		if ($member->getJoined() === '') {
121
			$this->membersRequest->createMember($member);
122
		} else {
123
			$this->membersRequest->updateMember($member);
124
		}
125
126
		$this->eventsService->onMemberNew($circle, $member);
127
	}
128
129
}
130
131