Completed
Push — master ( 0728e2...7bf439 )
by Tomas
10:58 queued 10s
created

StaticTokenRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validToken() 0 4 1
A ipRestrictions() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Misc;
6
7
/**
8
 * @todo change implements to TokenRepositoryInterface after BearerTokenRepositoryInterface will be removed in 3.0.0
9
 */
10
class StaticTokenRepository implements BearerTokenRepositoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Tomaj\NetteApi\Misc\BearerTokenRepositoryInterface has been deprecated with message: use TokenRepositoryInterface instead
will be removed in 3.0.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
11
{
12
    /**
13
     * array
14
     */
15
    private $validTokens = [];
16
17
    /**
18
     * Create static bearer token repository.
19
     * You can pass multiple tokens that will be available for your api.
20
     * Format is associtive array where key is token string and value is IP range
21
     *
22
     * Example:
23
     * ['ef0p9iwehjgoihrgrsdgfoihw4t' => '*']
24
     *
25
     * Or:
26
     * ['asfoihegoihregoihrhgrehg' => '127.0.0.1', 'asfo9uyewtoiyewgt4ty4r' => '*']
27
     *
28
     * @see BearerTokenAuthorization#isValidIp for all available Ip range formats
29
     *
30
     * @param array $validTokens
31
     */
32
    public function __construct($validTokens = [])
33
    {
34
        $this->validTokens = $validTokens;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function validToken(string $token): bool
41
    {
42
        return in_array($token, array_keys($this->validTokens));
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function ipRestrictions(string $token): ?string
49
    {
50
        if (isset($this->validTokens[$token])) {
51
            return $this->validTokens[$token];
52
        }
53
        return null;
54
    }
55
}
56