Passed
Push — master ( a3b0d7...035240 )
by Gabor
03:51
created

NameAndPasswordCredential::addCredential()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Auth\Credential;
15
16
use InvalidArgumentException;
17
use WebHemi\Adapter\Auth\AuthCredentialInterface;
18
19
/**
20
 * Interface NameAndPasswordCredential
21
 */
22
class NameAndPasswordCredential implements AuthCredentialInterface
23
{
24
    /** @var string */
25
    private $username = '';
26
    /** @var string */
27
    private $password = '';
28
29
    /**
30
     * Set a credential.
31
     *
32
     * @param string $key
33
     * @param string $value
34
     * @throws InvalidArgumentException
35
     * @return AuthCredentialInterface
36
     */
37 1
    public function addCredential(string $key, string $value) : AuthCredentialInterface
38
    {
39
        switch ($key) {
40 1
            case 'username':
41 1
                $this->username = $value;
42 1
                break;
43
44 1
            case 'password':
45 1
                $this->password = $value;
46 1
                break;
47
48
            default:
49 1
                throw new InvalidArgumentException(
50
                    sprintf(
51 1
                        'Parameter #1 must be either "usenrame" or "password", %s given.',
52
                        $key
53
                    ),
54 1
                    1000
55
                );
56
        }
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * Returns the credentials in a key => value array.
63
     *
64
     * @return array
65
     */
66 1
    public function getCredentials() : array
67
    {
68 1
        return ['username' => $this->username, 'password' => $this->password];
69
    }
70
}
71