UserService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 6
b 0
f 0
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 2
A login() 0 13 3
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