Passed
Push — master ( 20361a...82a2cb )
by Tobias
01:15
created

UserPasswordTrait::withUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Dsn\Configuration;
6
7
trait UserPasswordTrait
8
{
9
    /**
10
     * @var array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
11
     *             user: string|null,
12
     *             password: string|null,
13
     *             }
14
     */
15
    private $authentication = ['user' => null, 'password' => null];
16
17
    /**
18
     * @return array
19
     */
20
    public function getAuthentication()
21
    {
22
        return $this->authentication;
23
    }
24
25
    private function setAuthentication(array $authentication): void
26
    {
27
        if (!empty($authentication)) {
28
            $this->authentication = $authentication;
29
        }
30
    }
31
32
    public function getUser(): ?string
33
    {
34
        return $this->authentication['user'] ?? null;
35
    }
36
37
    public function withUser(?string $user): self
38
    {
39
        $new = clone $this;
40
        $new->authentication['user'] = $user;
41
42
        return $new;
43
    }
44
45
    public function getPassword(): ?string
46
    {
47
        return $this->authentication['password'] ?? null;
48
    }
49
50
    public function withPassword(?string $password): self
51
    {
52
        $new = clone $this;
53
        $new->authentication['password'] = $password;
54
55
        return $new;
56
    }
57
58
    private function getUserInfoString(): string
59
    {
60
        $user = $this->getUser() ?? '';
61
        $password = $this->getPassword() ?? '';
62
63
        if ('' === $password && '' === $user) {
64
            return '';
65
        }
66
67
        return $user.('' === $password ? '' : ':'.$password).'@';
68
    }
69
}
70