Completed
Push — master ( 1ee813...70e19a )
by John
02:44
created

validateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace LunixREST\AccessControl;
3
4
use LunixREST\Configuration\Configuration;
5
use LunixREST\APIRequest\APIRequest;
6
7
/**
8
 * Access control that uses a Configuration to read in valid keys. All keys the configuration provide are given full access
9
 * Class AllAccessListAccessControl
10
 * @package LunixRESTBasics\AccessControl
11
 */
12
class AllAccessConfigurationListAccessControl implements AccessControl
13
{
14
    /**
15
     * @var Configuration
16
     */
17
    protected $config;
18
19
    /**
20
     * @var string
21
     */
22
    protected $configKey;
23
    /**
24
     * @var string
25
     */
26
    private $namespace;
27
28
    /**
29
     * @param Configuration $config a config that has a list of valid keys in the stored $configKey
30
     * @param $namespace
31
     * @param string $configKey key to use when accessing the list of valid keys from the $config
32
     */
33 2
    public function __construct(Configuration $config, string $namespace, string $configKey = 'keys')
34
    {
35 2
        $this->config = $config;
36 2
        $this->configKey = $configKey;
37 2
        $this->namespace = $namespace;
38 2
    }
39
40
    /**
41
     * @param \LunixREST\APIRequest\APIRequest $request
42
     * @return bool true if this key is valid
43
     */
44 1
    public function validateAccess(APIRequest $request)
45
    {
46 1
        return $this->validateKey($request->getApiKey());
47
    }
48
49
    /**
50
     * @param $apiKey
51
     * @return bool true if the key is found inside of the array returned by the config when $config->get is called with the key from the constructor
52
     */
53 2
    public function validateKey($apiKey)
54
    {
55 2
        return in_array($apiKey, $this->config->get($this->configKey, $this->namespace));
56
    }
57
}
58