Completed
Push — 3.x ( e508bd...b32bc5 )
by Jeroen
204:55 queued 139:42
created

EmailChangeController::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 39
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 56
ccs 0
cts 45
cp 0
crap 20
rs 9.296

How to fix   Long Method   

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
namespace Elgg\Users;
4
5
use Elgg\Request;
6
use Elgg\Http\ResponseBuilder;
7
use Elgg\Email;
8
use Elgg\Email\Address;
9
10
/**
11
 * Controller to handle confirmation of a user e-mail address change
12
 *
13
 * @since 3.1
14
 */
15
class EmailChangeController {
16
	
17
	/**
18
	 * Execute a email change
19
	 *
20
	 * @param Request $request the HTTP request
21
	 *
22
	 * @return ResponseBuilder
23
	 */
24
	public function __invoke(Request $request) {
25
		$translator = $request->elgg()->translator;
26
		
27
		$user = $request->getEntityParam();
28
		if (!$user instanceof \ElggUser) {
29
			return elgg_error_response($translator->translate('error:missing_data'));
30
		}
31
		
32
		$new_email = $user->getPrivateSetting('new_email');
33
		if (empty($new_email)) {
34
			return elgg_error_response($translator->translate('account:email:request:error:no_new_email'));
35
		}
36
		
37
		$user->removePrivateSetting('new_email');
38
		
39
		try {
40
			$request->elgg()->accounts->assertValidEmail($new_email, true);
41
		} catch (\RegistrationException $e) {
42
			return elgg_error_response($e->getMessage());
43
		}
44
		
45
		$old_email = $user->email;
46
		$user->email = $new_email;
47
		$user->save();
48
		
49
		// notify old and new email of the change
50
		$site = elgg_get_site_entity();
51
		$notification_params = [
52
			'object' => $user,
53
			'action' => 'email_change',
54
		];
55
		
56
		$notification = Email::factory([
57
			'from' => $site,
58
			'to' => new Address($old_email, $user->getDisplayName()),
59
			'subject' => $translator->translate('email:confirm:email:old:subject', [], $user->getLanguage()),
60
			'body' => $translator->translate('email:confirm:email:old:body', [
61
				$user->getDisplayName(),
62
				$site->getDisplayName(),
63
				$new_email,
64
				$site->getURL(),
65
			], $user->getLanguage()),
66
			'params' => $notification_params,
67
		]);
68
		elgg_send_email($notification);
69
		
70
		$subject = $translator->translate('email:confirm:email:new:subject', [], $user->getLanguage());
71
		$body = $translator->translate('email:confirm:email:new:body', [
72
			$user->getDisplayName(),
73
			$site->getDisplayName(),
74
			$site->getURL(),
75
		], $user->getLanguage());
76
		
77
		notify_user($user->guid, $site->guid, $subject, $body, $notification_params, ['email']);
78
		
79
		return elgg_ok_response('', $translator->translate('email:save:success'), '');
80
	}
81
}
82