Completed
Push — master ( 5acd24...5c27f0 )
by John
14s
created

ArrayKeyRepository::findKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace LunixREST\Server\AccessControl\KeyRepository;
3
4
use LunixREST\Server\AccessControl\KeyRepository\Exceptions\UnableToFindKeyException;
5
6
/**
7
 * A key repository based on a provided array.
8
 * Class ArrayKeyRepository
9
 * @package LunixREST\Server\AccessControl\KeyRepository
10
 */
11
class ArrayKeyRepository implements KeyRepository
12
{
13
    /**
14
     * @var
15
     */
16
    private $arrayOfKeys;
17
18
    /**
19
     * ArrayKeyRepository constructor.
20
     * @param $arrayOfKeys
21
     */
22 8
    public function __construct($arrayOfKeys)
23
    {
24 8
        $this->arrayOfKeys = $arrayOfKeys;
25 8
    }
26
27
    /**
28
     * @param string $key
29
     * @return KeyMeta
30
     * @throws UnableToFindKeyException
31
     */
32 4
    public function findKey(string $key): KeyMeta
33
    {
34 4
        if($this->hasKey($key)) {
35 2
            return new KeyMeta($key);
36
        } else {
37 2
            throw new UnableToFindKeyException("Unable to find API key in array.");
38
        }
39
    }
40
41
    /**
42
     * @param string $key
43
     * @return bool
44
     */
45 8
    public function hasKey(string $key): bool
46
    {
47 8
        return in_array($key, $this->arrayOfKeys);
48
    }
49
}
50