DirectAdminChangePasswordDriver::ChangePassword()   C
last analyzed

Complexity

Conditions 14
Paths 42

Size

Total Lines 69

Duplication

Lines 23
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 14
nc 42
nop 3
dl 23
loc 69
rs 5.6896
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
class DirectAdminChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
	/**
6
	 * @var string
7
	 */
8
	private $sHost = '';
9
10
	/**
11
	 * @var string
12
	 */
13
	private $iPort = 2222;
14
	
15
	/**
16
	 * @var string
17
	 */
18
	private $sAllowedEmails = '';
19
20
	/**
21
	 * @var \MailSo\Log\Logger
22
	 */
23
	private $oLogger = null;
24
	
25
	/**
26
	 * @param string $sHost
27
	 * @param int $iPort
28
	 *
29
	 * @return \DirectAdminChangePasswordDriver
30
	 */
31
	public function SetConfig($sHost, $iPort)
32
	{
33
		$this->sHost = $sHost;
34
		$this->iPort = $iPort;
0 ignored issues
show
Documentation Bug introduced by
The property $iPort was declared of type string, but $iPort is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
35
36
		return $this;
37
	}
38
39
	/**
40
	 * @param string $sAllowedEmails
41
	 *
42
	 * @return \DirectAdminChangePasswordDriver
43
	 */
44
	public function SetAllowedEmails($sAllowedEmails)
45
	{
46
		$this->sAllowedEmails = $sAllowedEmails;
47
		return $this;
48
	}
49
50
	/**
51
	 * @param \MailSo\Log\Logger $oLogger
52
	 *
53
	 * @return \DirectAdminChangePasswordDriver
54
	 */
55
	public function SetLogger($oLogger)
56
	{
57
		if ($oLogger instanceof \MailSo\Log\Logger)
0 ignored issues
show
Bug introduced by
The class MailSo\Log\Logger does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
		{
59
			$this->oLogger = $oLogger;
60
		}
61
62
		return $this;
63
	}
64
65
	/**
66
	 * @param \RainLoop\Account $oAccount
67
	 *
68
	 * @return bool
69
	 */
70
	public function PasswordChangePossibility($oAccount)
71
	{
72
		return $oAccount && $oAccount->Email() &&
73
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
74
	}
75
76
	/**
77
	 * @param \RainLoop\Account $oAccount
78
	 * @param string $sPrevPassword
79
	 * @param string $sNewPassword
80
	 *
81
	 * @return bool
82
	 */
83
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
84
	{
85
		if ($this->oLogger)
86
		{
87
			$this->oLogger->Write('DirectAdmin: Try to change password for '.$oAccount->Email());
88
		}
89
90
		$bResult = false;
91
		if (!empty($this->sHost) && 0 < $this->iPort && $oAccount)
92
		{
93
			$sEmail = \trim(\strtolower($oAccount->Email()));
94
95
			$sHost = \trim($this->sHost);
96
			$sHost = \str_replace('{user:host-imap}', $oAccount->Domain()->IncHost(), $sHost);
97
			$sHost = \str_replace('{user:host-smtp}', $oAccount->Domain()->OutHost(), $sHost);
98
			$sHost = \str_replace('{user:domain}', \MailSo\Base\Utils::GetDomainFromEmail($sEmail), $sHost);
0 ignored issues
show
Unused Code introduced by
$sHost is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
			$sHost = \rtrim($this->sHost, '/\\');
100
			
101
			if (!\preg_match('/^http[s]?:\/\//i', $sHost))
102
			{
103
				$sHost = 'http://'.$sHost;
104
			}
105
106
			$sUrl = $sHost.':'.$this->iPort.'/CMD_CHANGE_EMAIL_PASSWORD';
107
108
			$iCode = 0;
109
			$oHttp = \MailSo\Base\Http::SingletonInstance();
110
111
			if ($this->oLogger)
112
			{
113
				$this->oLogger->Write('DirectAdmin[Api Request]:'.$sUrl);
114
			}
115
116
			$mResult = $oHttp->SendPostRequest($sUrl,
117
				array(
118
					'email'         => $sEmail,
119
					'oldpassword'   => $sPrevPassword,
120
					'password1'     => $sNewPassword,
121
					'password2'     => $sNewPassword,
122
					'api'           => '1'
123
				), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger);
124
125 View Code Duplication
			if (false !== $mResult && 200 === $iCode)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
			{
127
				$aRes = null;
128
				@\parse_str($mResult, $aRes);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
129
				if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1))
130
				{
131
					$bResult = true;
132
				}
133
				else
134
				{
135
					if ($this->oLogger)
136
					{
137
						$this->oLogger->Write('DirectAdmin[Error]: Response: '.$mResult);
138
					}
139
				}
140
			}
141
			else
142
			{
143
				if ($this->oLogger)
144
				{
145
					$this->oLogger->Write('DirectAdmin[Error]: Empty Response: Code:'.$iCode);
146
				}
147
			}
148
		}
149
150
		return $bResult;
151
	}
152
}