Completed
Push — 2.x ( aeb5ce...f5bd0f )
by Jindřich
03:11
created

FosUserRegistrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A registerUser() 0 14 1
A generatePassword() 0 7 1
1
<?php
2
3
namespace SkautisBundle\Security\Authentication\Registrator;
4
5
6
use Skautis\Skautis;
7
use FOS\UserBundle\Model\UserManager;
8
use SkautisBundle\Security\Authentication\Registrator\UserRegistratorInterface;
9
use Symfony\Component\Security\Core\Util\SecureRandom;
10
11
class FosUserRegistrator implements  UserRegistratorInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "UserRegistratorInterface"; 2 found
Loading history...
12
{
13
    const NUMBER_OF_RANDOM_BYTES = 10;
14
15
    /**
16
     * @var Skautis
17
     */
18
    protected $skautis;
19
20
    /**
21
     * @var UserManager
22
     */
23
    protected $userManager;
24
25
    /**
26
     * DoctrineRegistrator constructor.
27
     * @param Skautis $skautis
28
     * @param UserManager $userManager
29
     */
30
    public function __construct(Skautis $skautis, UserManager $userManager)
31
    {
32
        $this->skautis = $skautis;
33
        $this->userManager = $userManager;
34
    }
35
36
37
    /**
38
     * @return string Username of newly registered user
39
     */
40
    public function registerUser()
41
    {
42
        $data = $this->skautis->user->UserDetail();
43
44
        $user = $this->userManager->createUser();
45
        $user->setEnabled(true);
46
        $user->setUsername($data->UserName);
47
        $user->setPassword($this->generatePassword());
48
        $user->setEmail("[email protected]");
49
50
        $this->userManager->updateUser($user);
51
52
        return $user->getUsername();
53
    }
54
55
    /**
56
     * Generate password for newly registered user
57
     * @return string
58
     */
59
    protected function generatePassword()
60
    {
61
        $generator = new SecureRandom();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Security\Core\Util\SecureRandom has been deprecated with message: since version 2.8, to be removed in 3.0. Use the random_bytes function instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
62
        $randomBytes = $generator->nextBytes(self::NUMBER_OF_RANDOM_BYTES);
63
64
        return base64_encode($randomBytes);
65
    }
66
67
}
68