Passed
Push — master ( 069052...791c20 )
by Pierre
02:35
created

Users::auth()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
namespace App\Model;
4
5
use App\Config;
6
7
/**
8
 * Users class is a basic class to let auth process running.
9
 * It uses fake accounts come from config accounts key.
10
 * For security reason, don't use this in prod /!\
11
 */
12
class Users
13
{
14
15
    const _ID = 'id';
16
    const _NAME = 'name';
17
    const _EMAIL = 'email';
18
    const _PASSWORD = 'password';
19
    const _ROLE = 'role';
20
    const _VALID = 'valid';
21
    const _ACCOUNTS = 'accounts';
22
23
    /**
24
     * app config
25
     *
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * accounts list
32
     *
33
     * @var array
34
     */
35
    private $accounts;
36
37
    /**
38
     * instanciate
39
     *
40
     * @param Config $config
41
     */
42 3
    public function __construct(Config $config)
43
    {
44 3
        $this->config = $config;
45 3
        $this->accounts = $this->config->getSettings(self::_ACCOUNTS);
46
    }
47
48
    /**
49
     * auth a user for a given email and password
50
     *
51
     * @param string $email
52
     * @param string $password
53
     * @return array
54
     */
55 1
    public function auth(string $email, string $password): array
56
    {
57 1
        $acNumber = count($this->accounts);
58 1
        for ($c = 0; $c < $acNumber; $c++) {
59 1
            $user = $this->accounts[$c];
60 1
            if ($user[self::_EMAIL] === $email
61 1
                && $password === $user[self::_PASSWORD]
62
            ) {
63 1
                return $user;
64
            }
65
        }
66 1
        return [];
67
    }
68
69
    /**
70
     * return user array for a given user id
71
     *
72
     * @param integer $uid
73
     * @return array
74
     */
75 1
    public function getById(int $uid): array
76
    {
77 1
        return isset($this->accounts[$uid]) ? $this->accounts[$uid] : [];
78
    }
79
}
80