Completed
Push — master ( a52d30...b030ea )
by Korotkov
03:48
created

UserService::getUserRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 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
17
{
18
19
    /**
20
     * @var array
21
     */
22
    protected $users = [];
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", $name);
34
        }
35
36
        return sprintf("%s was already registered \n", $name);
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
            return sprintf("%s is authenticated \n", $name);
47
        }
48
49
        return sprintf("%s must be registered \n", $name);
50
    }
51
}
52