Completed
Push — master ( 5b3aee...7e6185 )
by Nicolas
12s
created

EncodePasswordExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Smart\AuthenticationBundle\Admin\Extension;
4
5
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
6
use Sonata\AdminBundle\Admin\AdminInterface;
7
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
10
/**
11
 * @author Mathieu Ducrot <[email protected]>
12
 */
13
class EncodePasswordExtension extends AbstractAdminExtension
14
{
15
    /**
16
     * @var UserPasswordEncoderInterface
17
     */
18
    private $encoder;
19
20
    /**
21
     * @param UserPasswordEncoderInterface $encoder
22
     */
23
    public function __construct(UserPasswordEncoderInterface $encoder)
24
    {
25
        $this->encoder = $encoder;
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function preUpdate(AdminInterface $admin, $user)
32
    {
33
        $this->encodePassword($user);
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function prePersist(AdminInterface $admin, $user)
40
    {
41
        $this->encodePassword($user);
42
    }
43
44
    /**
45
     * @param UserInterface $user
46
     */
47
    private function encodePassword(UserInterface $user)
48
    {
49
        if ("" === trim($user->getPlainPassword())) {
0 ignored issues
show
Bug introduced by
The method getPlainPassword() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
            return;
51
        }
52
53
        $user->setPassword($this->encoder->encodePassword($user, $user->getPlainPassword()));
0 ignored issues
show
Bug introduced by
The method getPlainPassword() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setPassword() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
    }
55
}
56