UserService::register()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
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
    private array $users        = [];
15
    private bool $authenticated = false;
16
17
    /**
18
     * Registers a new user
19
     * --------------------
20
     * Регистрирует нового пользователя
21
     * 
22
     * @param  string $name
23
     * @return string
24
     */
25
    public function register(string $name): string
26
    {
27
        if (!array_key_exists($name, $this->users)) {
28
            $this->users[$name] = new MemoryUser($name);
29
30
            return sprintf("%s has been registered \n", $this->users[$name]->getName());
31
        }
32
33
        return $this->login($this->users[$name]->getName());
34
    }
35
36
    /**
37
     * Identifies the user
38
     * -------------------
39
     * Идентифицирует  пользователя
40
     * 
41
     * @param  string $name
42
     * @return string
43
     */
44
    public function login(string $name): string
45
    {
46
        if (array_key_exists($name, $this->users)){
47
            if (!$this->authenticated) {
48
                $this->authenticated = true;
49
50
                return sprintf("%s is authenticated \n", $name);
51
            }
52
53
            return sprintf("%s was already authenticated \n", $name);
54
        }
55
56
        return sprintf("%s must be registered \n", $name);
57
    }
58
}
59