Completed
Pull Request — master (#4)
by Klochok
10:05
created

EmailConfirm::confirm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace hiam\mrdp\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\httpclient\Client;
8
9
/**
10
 * Class EmailConfirm is legacy code from old.ahnemes provided email confirmation.
11
 */
12
class EmailConfirm extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    public $email;
18
19
    /**
20
     * @var string
21
     */
22
    public $client;
23
24
    /**
25
     * @var integer
26
     */
27
    public $id;
28
29
    /**
30
     * @var string The API module name and the method name (in API call names manner) to which the request should be sent.
31
     * For example: `clientConfirmEmail` or `contactConfirmEmail`
32
     */
33
    public $what;
34
35
    /**
36
     * @var string
37
     */
38
    public $salt;
39
40
    /**
41
     * @var string
42
     */
43
    public $hash;
44
45
    /**
46
     * {@inheritdoc}
47
     * @return array
48
     */
49
    public function rules()
50
    {
51
        return [
52
            [['id', 'client', 'email', 'what', 'salt', 'hash'], 'required'],
53
            [['email'], 'email'],
54
            [['client', 'what', 'salt', 'hash'], 'string'],
55
            ['what', 'in', 'range' => ['clientConfirmEmail', 'contactConfirmEmail']],
56
        ];
57
    }
58
59
    /**
60
     * Send http request to API
61
     * @return string|bool
62
     */
63
    public function confirm()
64
    {
65
        $error = null;
0 ignored issues
show
Unused Code introduced by
$error 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...
66
        if (!$this->validate()) {
67
            return implode("\n", $this->getFirstErrors());
68
        }
69
        $client = new Client(['baseUrl' => Yii::$app->params['hiapi.url']]);
70
        $response = $client->createRequest()
71
            ->setUrl($this->what)
72
            ->setData(['confirm_data' => $this->attributes])
73
            ->send();
74
        $responseData = $response->getData();
75
        if (array_key_exists('_error', $responseData)) {
76
            return $responseData['_error'];
77
        }
78
79
        return true;
80
    }
81
}
82
83