Test Failed
Push — master ( 7e45f2...9f50b7 )
by Pierre
15:19
created

Users   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
ccs 12
cts 12
cp 1
rs 10
wmc 6

3 Methods

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