Passed
Push — master ( 1b37b7...612acf )
by Tobias
01:49
created

UserPasswordTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 12
c 1
b 0
f 1
dl 0
loc 45
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthentication() 0 3 1
A setAuthentication() 0 4 2
A getPassword() 0 3 1
A getUser() 0 3 1
A getUserInfoString() 0 10 4
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 getPassword(): ?string
38
    {
39
        return $this->authentication['password'] ?? null;
40
    }
41
42
    private function getUserInfoString(): string
43
    {
44
        $user = $this->getUser() ?? '';
45
        $password = $this->getPassword() ?? '';
46
47
        if ('' === $password && '' === $user) {
48
            return '';
49
        }
50
51
        return $user.('' === $password ? '' : ':'.$password).'@';
52
    }
53
}
54