Failed Conditions
Push — master ( b69c62...c6060f )
by Sébastien
02:49
created

ContredanseUserProvider::getAllUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Security;
6
7
use App\Security\Exception\UserNotFoundException;
8
use Zend\Expressive\Authentication\UserInterface;
9
10
class ContredanseUserProvider implements UserProviderInterface
11
{
12
    /**
13
     * @var \PDO
14
     */
15
    private $adapter;
16
17
    public function __construct(\PDO $adapter)
18
    {
19
        $this->adapter = $adapter;
20
    }
21
22
    public function getUserByEmail(string $email): ?UserInterface
23
    {
24
        $sql = sprintf(
25
            "%s\n%s",
26
            $this->getBaseSql(),
27
            'where `l`.`Login` = :email'
28
        );
29
        $stmt = $this->adapter->prepare(
30
            $sql,
31
            [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]
32
        );
33
        $stmt->execute([':email' => $email]);
34
        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
35
        if (!$rows || count($rows) !== 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rows of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
            return null;
37
        }
38
39
        return new ContredanseUser(
40
            $rows[0]['user_id'],
41
            explode(' ', $rows[0]['role'] ?? ''),
42
            $rows[0]
43
        );
44
    }
45
46
    /**
47
     * Return all users.
48
     *
49
     * @return array|false
50
     */
51
    public function getAllUsers()
52
    {
53
        $sql  = $this->getBaseSql();
54
        $stmt = $this->adapter->prepare(
55
            $sql,
56
            [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]
57
        );
58
        $stmt->execute();
59
60
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
61
    }
62
63
    /**
64
     * Return a specific user.
65
     *
66
     * @return array|false
67
     */
68
    public function findUser(string $user_id)
69
    {
70
        $sql = sprintf(
71
            "%s\n%s",
72
            $this->getBaseSql(),
73
            'where `l`.`user_id` = :user_id'
74
        );
75
        $stmt = $this->adapter->prepare(
76
            $sql,
77
            [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]
78
        );
79
        $stmt->execute([':user_id' => $user_id]);
80
        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
81
        if (!$rows || count($rows) !== 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rows of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
            throw new UserNotFoundException(sprintf(
83
                'User \'%d\' not found',
84
                $user_id
85
            ));
86
        }
87
88
        return $rows[0];
89
    }
90
91
    private function getBaseSql(): string
92
    {
93
        $sql = '
94
                  select 
95
                      `s`.`suj_id` as `subject_id`,
96
                      `l`.`User_id` as `user_id`, 
97
                      `l`.`Login` as `email`, 
98
                      `l`.`Pwd` as `password`, 
99
                      `s`.`suj_type` as `subject_type`,
100
                      `s`.`suj_title` as `title`,
101
                      `s`.`suj_name` as `name`,
102
                      `s`.`suj_firstname` as `firstname`,                      
103
                      `l`.`role`,
104
                      `s`.`membre_cd`,
105
                      `s`.`membre_cf`
106
                  from `usr_login` as `l` inner join `sujet` as `s` 
107
                  on `s`.`suj_id` = `l`.`suj_id`  
108
               ';
109
110
        return $sql;
111
    }
112
}
113