Passed
Push — master ( 939347...72ceee )
by Mihail
07:40
created

FormRecovery   B

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 16
dl 0
loc 87
rs 8.4614

3 Methods

Rating   Name   Duplication   Size   Complexity  
A labels() 0 7 1
A rules() 0 9 1
C make() 0 51 7
1
<?php
2
3
namespace Apps\Model\Front\User;
4
5
use Apps\ActiveRecord\UserRecovery;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Exception\SyntaxException;
9
use Ffcms\Core\Helper\Date;
10
use Ffcms\Core\Helper\Type\Str;
11
12
class FormRecovery extends Model
13
{
14
    const DELAY = 900; // delay between 2 recovery submits
15
16
    public $email;
17
    public $captcha;
18
19
    /**
20
    * Labels
21
    */
22
    public function labels()
23
    {
24
        return [
25
            'email' => __('Email'),
26
            'captcha' => __('Captcha')
27
        ];
28
    }
29
30
    /**
31
    * Validation rules
32
    */
33
    public function rules()
34
    {
35
        return [
36
            [['email', 'captcha'], 'required'],
37
            ['email', 'email'],
38
            ['captcha', 'App::$Captcha::validate'],
39
            ['email', 'App::$User::isMailExist']
40
        ];
41
    }
42
43
    /**
44
     * After validation generate new pwd, recovery token and send email
45
     * @throws SyntaxException
46
     */
47
    public function make()
48
    {
49
        $user = App::$User->getIdentityViaEmail($this->email);
50
        if ($user === null) {
51
            throw new SyntaxException('Email not found');
52
        }
53
        if ($user->approve_token !== '0' && Str::length($user->approve_token) > 0) {
0 ignored issues
show
Bug introduced by
Accessing approve_token on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
            throw new SyntaxException('You must approve your account');
55
        }
56
57
        $rows = UserRecovery::where('user_id', '=', $user->getId())
58
            ->orderBy('id', 'DESC')
59
            ->first();
60
61
        if ($rows !== null && $rows !== false) {
62
            // prevent spam of recovery messages
63
            if (Date::convertToTimestamp($rows->created_at) > time() - self::DELAY) {
64
                return;
65
            }
66
        }
67
68
        // generate pwd, token and pwdCrypt
69
        $newPwd = Str::randomLatinNumeric(mt_rand(8, 16));
70
        $pwdCrypt = App::$Security->password_hash($newPwd);
71
        $token = Str::randomLatinNumeric(mt_rand(64, 128));
72
73
        $rObject = new UserRecovery();
74
        $rObject->user_id = $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
        $rObject->password = $pwdCrypt;
76
        $rObject->token = $token;
77
        $rObject->save();
78
79
        // generate mail template
80
        $mailTemplate = App::$View->render('user/_recoveryMail', [
81
            'login' => $user->login,
0 ignored issues
show
Bug introduced by
Accessing login on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
82
            'email' => $this->email,
83
            'password' => $newPwd,
84
            'token' => $token,
85
            'id' => $rObject->id
86
        ]);
87
88
        $sender = App::$Properties->get('adminEmail');
89
90
        // format SWIFTMailer format
91
        $mailMessage = \Swift_Message::newInstance(App::$Translate->get('Profile', 'Account recovery on %site%', ['site' => App::$Request->getHost()]))
92
            ->setFrom([$sender])
93
            ->setTo([$this->email])
94
            ->setBody($mailTemplate, 'text/html');
95
        // send message
96
        App::$Mailer->send($mailMessage);
97
    }
98
}