CpanelChangePasswordDriver::ChangePassword()   F
last analyzed

Complexity

Conditions 18
Paths 208

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 208
nop 3
dl 0
loc 84
rs 3.9333
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 CpanelChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
	/**
6
	 * @var string
7
	 */
8
	private $sHost = '';
9
10
	/**
11
	 * @var int
12
	 */
13
	private $iPost = 2087;
14
15
	/**
16
	 * @var bool
17
	 */
18
	private $bSsl = true;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $sUser = '';
24
25
	/**
26
	 * @var string
27
	 */
28
	private $sPassword = '';
29
30
	/**
31
	 * @var string
32
	 */
33
	private $sAllowedEmails = '';
34
35
	/**
36
	 * @var \MailSo\Log\Logger
37
	 */
38
	private $oLogger = null;
39
40
	/**
41
	 * @param string $sHost
42
	 * @param int $iPost
43
	 * @param bool $bSsl
44
	 * @param string $sUser
45
	 * @param string $sPassword
46
	 *
47
	 * @return \CpanelChangePasswordDriver
48
	 */
49
	public function SetConfig($sHost, $iPost, $bSsl, $sUser, $sPassword)
50
	{
51
		$this->sHost = $sHost;
52
		$this->iPost = $iPost;
53
		$this->bSsl = !!$bSsl;
54
		$this->sUser = $sUser;
55
		$this->sPassword = $sPassword;
56
57
		return $this;
58
	}
59
60
	/**
61
	 * @param string $sAllowedEmails
62
	 *
63
	 * @return \CpanelChangePasswordDriver
64
	 */
65
	public function SetAllowedEmails($sAllowedEmails)
66
	{
67
		$this->sAllowedEmails = $sAllowedEmails;
68
		return $this;
69
	}
70
71
	/**
72
	 * @param \MailSo\Log\Logger $oLogger
73
	 *
74
	 * @return \CpanelChangePasswordDriver
75
	 */
76
	public function SetLogger($oLogger)
77
	{
78
		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...
79
		{
80
			$this->oLogger = $oLogger;
81
		}
82
83
		return $this;
84
	}
85
86
	/**
87
	 * @param \RainLoop\Model\Account $oAccount
88
	 *
89
	 * @return bool
90
	 */
91
	public function PasswordChangePossibility($oAccount)
92
	{
93
		return $oAccount && $oAccount->Email() &&
94
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
95
	}
96
97
	/**
98
	 * @param \RainLoop\Model\Account $oAccount
99
	 * @param string $sPrevPassword
100
	 * @param string $sNewPassword
101
	 *
102
	 * @return bool
103
	 */
104
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
105
	{
106
		if ($this->oLogger)
107
		{
108
			$this->oLogger->Write('Try to change password for '.$oAccount->Email());
109
		}
110
111
		if (!\class_exists('xmlapi'))
112
		{
113
			include_once __DIR__.'/xmlapi.php';
114
		}
115
116
		$bResult = false;
117
		if (!empty($this->sHost) && 0 < $this->iPost &&
118
			0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) &&
119
			$oAccount && \class_exists('xmlapi'))
120
		{
121
			$sEmail = $oAccount->Email();
122
			$sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail);
123
			$sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail);
124
125
			$sHost = $this->sHost;
126
			$sHost = \str_replace('{user:domain}', $sEmailDomain, $sHost);
127
128
			$sUser = $this->sUser;
129
			$sUser = \str_replace('{user:email}', $sEmail, $sUser);
130
			$sUser = \str_replace('{user:login}', $sEmailUser, $sUser);
131
132
			$sPassword = $this->sPassword;
133
			$sPassword = \str_replace('{user:password}', $oAccount->Password(), $sPassword);
134
135
			try
136
			{
137
				$oXmlApi = new \xmlapi($sHost);
138
				$oXmlApi->set_port($this->iPost);
139
				$oXmlApi->set_protocol($this->bSsl ? 'https' : 'http');
140
				$oXmlApi->set_debug(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
				$oXmlApi->set_output('json');
142
//				$oXmlApi->set_http_client('fopen');
143
				$oXmlApi->set_http_client('curl');
144
				$oXmlApi->password_auth($sUser, $sPassword);
145
146
				$aArgs = array(
147
					'email' => $sEmailUser,
148
					'domain' => $sEmailDomain,
149
					'password' => $sNewPassword
150
				);
151
152
				$sResult = $oXmlApi->api2_query($sUser, 'Email', 'passwdpop', $aArgs);
153
				if ($sResult)
154
				{
155
					if ($this->oLogger)
156
					{
157
						$this->oLogger->Write('CPANEL: '.$sResult, \MailSo\Log\Enumerations\Type::INFO);
158
					}
159
160
					$aResult = @\json_decode($sResult, true);
161
					$bResult = isset($aResult['cpanelresult']['data'][0]['result']) &&
162
						!!$aResult['cpanelresult']['data'][0]['result'];
163
				}
164
165
				if (!$bResult && $this->oLogger)
166
				{
167
					$this->oLogger->Write('CPANEL: '.$sResult, \MailSo\Log\Enumerations\Type::ERROR);
168
				}
169
			}
170
			catch (\Exception $oException)
171
			{
172
				if ($this->oLogger)
173
				{
174
					$this->oLogger->WriteException($oException);
175
				}
176
			}
177
		}
178
		else
179
		{
180
			if ($this->oLogger)
181
			{
182
				$this->oLogger->Write('CPANEL: Incorrent configuration data', \MailSo\Log\Enumerations\Type::ERROR);
183
			}
184
		}
185
186
		return $bResult;
187
	}
188
}