UserProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 23
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A supportsClass() 0 4 2
A refreshUser() 0 7 2
A loadUserByUsername() 0 23 3
1
<?php
2
namespace PhpDraft\Config\Security;
3
4
use Silex\Application;
5
use Symfony\Component\Security\Core\User\UserProviderInterface;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
use Symfony\Component\Security\Core\User\User;
8
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use PhpDraft\Domain\Entities\LoginUser;
11
 
12
class UserProvider implements UserProviderInterface
13
{
14
  private $app;
15
16
  public function __construct(Application $app) {
17
    $this->app = $app;
18
  }
19
20
  public function loadUserByUsername($email)
21
  {
22
    //Won't use repository here because we need to throw the UsernameNotFoundException to kick off Symfony denying the request
23
    $user = new LoginUser();
24
    $user_stmt = $this->app['db']->prepare("SELECT * FROM users WHERE email = ? LIMIT 1");
25
    $user_stmt->setFetchMode(\PDO::FETCH_INTO, $user);
26
    $user_stmt->bindParam(1, $email);
27
28
    if (!$user_stmt->execute()) {
29
          throw new UsernameNotFoundException(sprintf('Email "%s" does not exist.', $email));
30
    }
31
32
    if (!$user_stmt->fetch()) {
33
          throw new UsernameNotFoundException(sprintf('Email "%s" does not exist.', $email));
34
    }
35
36
    return new PhpDraftSecurityUser($user->email,
37
      $user->name,
38
      $user->password, 
39
      $user->salt, 
40
      explode(',', $user->roles), 
41
      $user->enabled, 
42
      $user->verificationKey);
43
  }
44
45
  public function refreshUser(UserInterface $user)
46
  {
47
    if (!$user instanceof User) {
48
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
49
    }
50
51
    return $this->loadUserByUsername($user->getUsername());
52
  }
53
54
  public function supportsClass($class)
55
  {
56
    return $this->userRepository->getClassName() === $class
0 ignored issues
show
Bug Best Practice introduced by
The property userRepository does not exist on PhpDraft\Config\Security\UserProvider. Did you maybe forget to declare it?
Loading history...
57
      || is_subclass_of($class, $this->userRepository->getClassName());
58
    //return $class === 'Symfony\Component\Security\Core\User\User';
59
  }
60
}