Completed
Push — master ( 995cbb...c18ab0 )
by Korotkov
08:09 queued 01:09
created

UserService::getUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author  : Jagepard <[email protected]>
7
 * @license https://mit-license.org/ MIT
8
 */
9
10
namespace Creational\LazyInitialization;
11
12
class UserService implements ServiceInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $users = [];
18
19
    /**
20
     * @var bool
21
     */
22
    private $authenticated = false;
23
24
    /**
25
     * @param string $name
26
     * @return string
27
     */
28
    public function register(string $name): string
29
    {
30
        if (!array_key_exists($name, $this->users)) {
31
            $this->users[$name] = new MemoryUser($name);
32
33
            return sprintf("%s has been registered \n", $this->users[$name]->getUserName());
34
        }
35
36
        return $this->login($this->users[$name]->getUserName());
37
    }
38
39
    /**
40
     * @param string $name
41
     * @return string
42
     */
43
    public function login(string $name): string
44
    {
45
        if (array_key_exists($name, $this->users)){
46
            if (!$this->authenticated) {
47
                $this->authenticated = true;
48
49
                return sprintf("%s is authenticated \n", $name);
50
            }
51
52
            return sprintf("%s was already authenticated \n", $name);
53
        }
54
55
        return sprintf("%s must be registered \n", $name);
56
    }
57
}
58