SecureEmailChangeStrategy::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 24
cp 0
rs 9.472
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 20
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\Strategy;
13
14
use Da\User\Contracts\MailChangeStrategyInterface;
15
use Da\User\Factory\MailFactory;
16
use Da\User\Factory\TokenFactory;
17
use Da\User\Form\SettingsForm;
18
use Da\User\Model\User;
19
use Da\User\Traits\ContainerAwareTrait;
20
use Yii;
21
22
class SecureEmailChangeStrategy implements MailChangeStrategyInterface
23
{
24
    use ContainerAwareTrait;
25
26
    protected $form;
27
28
    public function __construct(SettingsForm $form)
29
    {
30
        $this->form = $form;
31
    }
32
33
    public function run()
34
    {
35
        if ($this->make(DefaultEmailChangeStrategy::class, [$this->form])->run()) {
36
            $token = TokenFactory::makeConfirmOldMailToken($this->form->getUser()->id);
0 ignored issues
show
Bug introduced by
Accessing id 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...
37
            $mailService = MailFactory::makeReconfirmationMailerService($this->form->getUser(), $token);
0 ignored issues
show
Compatibility introduced by
$this->form->getUser() of type object<yii\web\IdentityInterface> is not a sub-type of object<Da\User\Model\User>. It seems like you assume a concrete implementation of the interface yii\web\IdentityInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
38
39
            if ($mailService->run()) {
40
                // unset flags if they exist
41
                $this->form->getUser()->flags &= ~User::NEW_EMAIL_CONFIRMED;
42
                $this->form->getUser()->flags &= ~User::OLD_EMAIL_CONFIRMED;
43
                if ($this->form->getUser()->save(false)) {
44
                    Yii::$app
45
                        ->session
46
                        ->setFlash(
47
                            'info',
48
                            Yii::t(
49
                                'usuario',
50
                                'We have sent confirmation links to both old and new email addresses. You must click both links to complete your request.'
51
                            )
52
                        );
53
54
                    return true;
55
                }
56
            }
57
        }
58
59
        return false;
60
    }
61
}
62