VestaChangePasswordDriver::SetConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
class VestaChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
    /**
6
     * @var string
7
     */
8
    private $sHost = '';
9
10
    /**
11
     * @var string
12
     */
13
    private $iPort = 8083;
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 \VestaChangePasswordDriver
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 \VestaChangePasswordDriver
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 \VestaChangePasswordDriver
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('Vesta: 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
            $sHost = 'https://'.$sHost;
101
102
            $sUrl = $sHost.':'.$this->iPort.'/reset/mail/';
103
104
            $iCode = 0;
105
            $oHttp = \MailSo\Base\Http::SingletonInstance();
106
107
            if ($this->oLogger)
108
            {
109
                $this->oLogger->Write('Vesta[Api Request]:'.$sUrl);
110
            }
111
112
            $mResult = $oHttp->SendPostRequest($sUrl,
113
                array(
114
                    'email'         => $sEmail,
115
                    'password'   => $sPrevPassword,
116
                    'new'     => $sNewPassword,
117
                ), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger);
118
119 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...
120
            {
121
                $aRes = null;
122
                @\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...
123
                if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1))
124
                {
125
                    $bResult = true;
126
                }
127
                else
128
                {
129
                    if ($this->oLogger)
130
                    {
131
                        $this->oLogger->Write('Vesta[Error]: Response: '.$mResult);
132
                    }
133
                }
134
            }
135
            else
136
            {
137
                if ($this->oLogger)
138
                {
139
                    $this->oLogger->Write('Vesta[Error]: Empty Response: Code:'.$iCode);
140
                }
141
            }
142
        }
143
144
        return $bResult;
145
    }
146
}
147