UserRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 113
ccs 27
cts 32
cp 0.8438
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 9 2
A findByName() 0 8 2
A findByNameAndPassword() 0 13 3
A updateUser() 0 7 1
A removeUser() 0 4 1
A encode() 0 11 1
1
<?php
2
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace App\Repository;
15
16
use Component\Firewall\Firewall;
17
18
use App\Model\User;
19
20
/**
21
 * UserRepository.
22
 */
23
class UserRepository
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $database = [
29
        'user1' => [
30
            'password' => '$2y$11$08Snn524ll7Il70xB6aYS.T2kYt57owRzePDYQ.J78vjDjAj22WA6',
31
            'roles' => ['ROLE_PAGE_1', 'ROLE_API_READ'],
32
        ],
33
        'user2' => [
34
35
            'password' => '$2y$11$08Snn524ll7Il70xB6aYS.wVlOEQAdvMt4dOE52YSVJo6LhSPebcO',
36
            'roles' => ['ROLE_PAGE_2', 'ROLE_API_READ'],
37
        ],
38
        'user3' => [
39
            'password' => '$2y$11$08Snn524ll7Il70xB6aYS.VtYPqIhrXpzIw1nsbSkmOGA8nvTjdxS',
40
            'roles' => ['ROLE_PAGE_3', 'ROLE_API_READ'],
41
        ],
42
        'admin' => [
43
            'password' => '$2y$11$08Snn524ll7Il70xB6aYS.8aU.gMxuV0NqHQAlvXz3th85.1zjI5C',
44
            'roles' => [Firewall::ROLE_ADMIN],
45
        ],
46
    ];
47
48
    /**
49
     * @return array
50
     */
51 4
    public function findAll()
52
    {
53 4
        $users = [];
54 4
        foreach ($this->database as $key => $user) {
55 4
            $users[] = new User($key, $user['roles']);
56 4
        }
57
58 4
        return $users;
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return User|bool
65
     */
66 1
    public function findByName($name)
67
    {
68 1
        if (!isset($this->database[$name])) {
69
            return false;
70
        }
71
72 1
        return new User($name, $this->database[$name]['roles']);
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param string $password
78
     *
79
     * @return User|bool
80
     */
81 10
    public function findByNameAndPassword($name, $password)
82
    {
83 10
        if (!isset($this->database[$name])) {
84 1
            return false;
85
        }
86 9
        $data = $this->database[$name];
87
88 9
        if (!password_verify($password, $data['password'])) {
89
            return false;
90
        }
91
92 9
        return new User($name, $data['roles']);
93
    }
94
95
    /**
96
     * Sorry this changes will be not persisted.
97
     *
98
     * @param User   $user
99
     * @param string $password
100
     */
101 2
    public function updateUser(User $user, $password)
102
    {
103 2
        $this->database[$user->getName()] = [
104 2
            'password' => $this->encode($password),
105 2
            'roles' => $user->getRoles(),
106
        ];
107 2
    }
108
109
    /**
110
     * Sorry this changes will be not persisted.
111
     *
112
     * @param string $name
113
     */
114
    public function removeUser($name)
115
    {
116
        unset($this->database[$name]);
117
    }
118
119
    /**
120
     * @param string $password
121
     *
122
     * @return bool|string
123
     */
124 2
    protected function encode($password)
125
    {
126 2
        return password_hash(
127 2
            $password,
128 2
            PASSWORD_BCRYPT,
129
            [
130 2
                'cost' => 11,
131 2
                'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
132
            ]
133 2
        );
134
    }
135
}
136