Completed
Push — master ( 2416e6...bf1571 )
by Tomas
06:00
created

StaticBearerTokenRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi\Misc;
4
5
class StaticBearerTokenRepository implements BearerTokenRepositoryInterface
6
{
7
    /**
8
     * array
9
     */
10
    private $validTokens = [];
11
12
    /**
13
     * Create static bearer token repository.
14
     * You can pass multiple tokens that will be available for your api.
15
     * Format is associtive array where key is token string and value is IP range
16
     *
17
     * Example:
18
     * ['ef0p9iwehjgoihrgrsdgfoihw4t' => '*']
19
     *
20
     * Or:
21
     * ['asfoihegoihregoihrhgrehg' => '127.0.0.1', 'asfo9uyewtoiyewgt4ty4r' => '*']
22
     *
23
     * @see BearerTokenAuthorization#isValidIp for all available Ip range formats
24
     *
25
     * @param array $validTokens
26
     */
27 33
    public function __construct($validTokens = [])
28
    {
29 33
        $this->validTokens = $validTokens;
30 33
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 21
    public function validToken($token)
36
    {
37 21
        return in_array($token, array_keys($this->validTokens));
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 21
    public function ipRestrictions($token)
44
    {
45 21
        if (isset($this->validTokens[$token])) {
46 18
            return $this->validTokens[$token];
47
        }
48 3
        return false;
49
    }
50
}
51