Completed
Push — master ( dd449b...08bdaa )
by Korotkov
03:16 queued 01:32
created

UserService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 2
A login() 0 7 2
A getUsers() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @license   https://mit-license.org/ MIT
8
 */
9
10
namespace Creational\LazyInitialization;
11
12
/**
13
 * Class UserService
14
 * @package Creational\LazyInitialization
15
 */
16
class UserService implements ServiceInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    private $users = [];
22
23
    /**
24
     * @param string $name
25
     * @return string
26
     */
27
    public function register(string $name): string
28
    {
29
        if (!array_key_exists($name, $this->getUsers())) {
30
            $this->users[$name] = new MemoryUser($name);
31
32
            return sprintf("%s has been registered \n", $this->getUsers()[$name]->getName());
33
        }
34
35
        return sprintf("%s was already registered \n", $this->getUsers()[$name]->getName());
36
    }
37
38
    /**
39
     * @param string $name
40
     * @return string
41
     */
42
    public function login(string $name): string
43
    {
44
        if (array_key_exists($name, $this->getUsers())){
45
            return sprintf("%s is authenticated \n", $name);
46
        }
47
48
        return sprintf("%s must be registered \n", $name);
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getUsers(): array
55
    {
56
        return $this->users;
57
    }
58
}
59