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

UserService::getUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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