GdprDeleteForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Form;
13
14
use Da\User\Helper\SecurityHelper;
15
use Da\User\Model\User;
16
use Da\User\Traits\ContainerAwareTrait;
17
use Yii;
18
use yii\base\Model;
19
20
/**
21
 * Class GdprDeleteForm
22
 * @package Da\User\Form
23
 */
24
class GdprDeleteForm extends Model
25
{
26
    use ContainerAwareTrait;
27
28
    /**
29
     * @var string User's password
30
     */
31
    public $password;
32
    /**
33
     * @var SecurityHelper
34
     */
35
    protected $securityHelper;
36
    /**
37
     * @var User
38
     */
39
    protected $user;
40
41
    /**
42
     * @param SecurityHelper $securityHelper
43
     * @param array          $config
44
     */
45 1
    public function __construct(SecurityHelper $securityHelper, $config = [])
46
    {
47 1
        $this->securityHelper = $securityHelper;
48 1
        parent::__construct($config);
49 1
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function rules()
55
    {
56
        return [
57 1
            'requiredFields' => [['password'], 'required'],
58
            'passwordValidate' => [
59 1
                'password',
60
                function ($attribute) {
61 1
                    if (!$this->securityHelper
62 1
                        ->validatePassword($this->password, $this->getUser()->password_hash)
0 ignored issues
show
Bug introduced by
Accessing password_hash on the interface yii\web\IdentityInterface 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...
63
                    ) {
64 1
                        $this->addError($attribute, Yii::t('usuario', 'Invalid password'));
65
                    }
66 1
                },
67
            ]
68
        ];
69
    }
70
71
    /**
72
     * @return User|null|\yii\web\IdentityInterface
73
     */
74 1
    public function getUser()
75
    {
76 1
        if ($this->user == null) {
77 1
            $this->user = Yii::$app->user->identity;
78
        }
79
80 1
        return $this->user;
81
    }
82
83 1
    public function attributeLabels()
84
    {
85
        return [
86 1
            'password' => Yii::t('usuario', 'Password'),
87
        ];
88
    }
89
}
90