Credentials::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 8.8571
cc 5
eloc 11
nc 4
nop 1
crap 5
1
<?php
2
namespace Wonnova\SDK\Auth;
3
4
use Wonnova\SDK\Exception\InvalidArgumentException;
5
6
/**
7
 * Class Credentials
8
 * @author Wonnova
9
 * @link http://www.wonnova.com
10
 */
11
class Credentials implements CredentialsInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $key;
17
18
    /**
19
     * @param string|array $key
20
     */
21 48
    public function __construct($key)
22
    {
23 48
        if (is_array($key)) {
24 3
            if (! isset($key['key'])) {
25 1
                throw new InvalidArgumentException('Provided array doesn\'t include a "key" element');
26
            }
27
28 2
            $this->key = $key['key'];
29 47
        } elseif (is_string($key)) {
30 44
            $this->key = $key;
31 44
        } else {
32 1
            throw new InvalidArgumentException(sprintf(
33 1
                'Expected "array" or "string", provided "%s"',
34 1
                is_object($key) ? get_class($key) : gettype($key)
35 1
            ));
36
        }
37 46
    }
38
39
    /**
40
     * @return string
41
     */
42 46
    public function getKey()
43
    {
44 46
        return $this->key;
45
    }
46
47
    /**
48
     * @return array
49
     */
50 42
    public function toArray()
51
    {
52 42
        return ['key' => $this->getKey()];
53
    }
54
}
55