Users   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 20
c 2
b 0
f 1
dl 0
loc 67
ccs 15
cts 15
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A auth() 0 13 4
A getById() 0 6 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Model;
6
7
use Nymfonya\Component\Config;
8
use App\Component\Auth\AuthInterface;
9
10
/**
11
 * Users class is a basic class to let auth process running.
12
 * It uses fake accounts come from config accounts key.
13
 * For security reason, don't use this in prod /!\
14
 */
15
class Users implements AuthInterface
16
{
17
18
    const _NAME = 'name';
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
            if (
61 1
                $user[self::_EMAIL] === $email
62 1
                && $password === $user[self::_PASSWORD]
63
            ) {
64 1
                return $user;
65
            }
66
        }
67 1
        return [];
68
    }
69
70
    /**
71
     * return user array for a given user id
72
     *
73
     * @param integer $uid
74
     * @return array
75
     */
76 1
    public function getById(int $uid): array
77
    {
78
        $userById = array_filter($this->accounts, function ($user) use ($uid) {
79 1
            return $user['id'] === $uid;
80 1
        });
81 1
        return $userById;
82
    }
83
}
84