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

StaticBearerTokenRepository::ipRestrictions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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