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

StaticTokenRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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