Token::generateHttpHeaders()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
class Token
6
{
7
    /**
8
     * The Client instance.
9
     *
10
     * @var \ElfSundae\Laravel\Api\Client
11
     */
12
    protected $client;
13
14
    /**
15
     * Constructor.
16
     *
17
     * @param  \ElfSundae\Laravel\Api\Client  $client
18
     */
19
    public function __construct(Client $client)
20
    {
21
        $this->client = $client;
22
    }
23
24
    /**
25
     * Get the Client instance.
26
     *
27
     * @return \ElfSundae\Laravel\Api\Client
28
     */
29
    public function getClient()
30
    {
31
        return $this->client;
32
    }
33
34
    /**
35
     * Set the Client instance.
36
     *
37
     * @param  \ElfSundae\Laravel\Api\Client  $client
38
     */
39
    public function setClient(Client $client)
40
    {
41
        $this->client = $client;
42
    }
43
44
    /**
45
     * Generate an api token.
46
     *
47
     * @param  string  $key
48
     * @param  string  $secret
49
     * @param  string|int  $time
50
     * @return string|null
51
     */
52
    public function generate($key, $secret, $time)
53
    {
54
        if ($key && $secret && $time) {
55
            return substr(md5($key.$secret.$time), 10, 20);
56
        }
57
    }
58
59
    /**
60
     * Generate an api token for the given app key.
61
     *
62
     * @param  string  $key
63
     * @param  string|int  $time
64
     * @return string|null
65
     */
66
    public function generateForKey($key, $time)
67
    {
68
        if ($secret = $this->client->getAppSecretForKey($key)) {
69
            return $this->generate($key, $secret, $time);
70
        }
71
    }
72
73
    /**
74
     * Verify an api token.
75
     *
76
     * @param  string  $token
77
     * @param  string  $key
78
     * @param  string|int  $time
79
     * @return bool
80
     */
81
    public function verify($token, $key, $time)
82
    {
83
        return $token && $key && $time && $token === $this->generateForKey($key, $time);
84
    }
85
86
    /**
87
     * Generate a token data array.
88
     *
89
     * @param  string  $key
90
     * @param  string  $secret
91
     * @param  string|int|null  $time
92
     * @return array
93
     */
94
    public function generateData($key, $secret, $time = null)
95
    {
96
        $time = $time ?: time();
97
        $token = $this->generate($key, $secret, $time);
98
99
        return $token ? compact('key', 'time', 'token') : [];
100
    }
101
102
    /**
103
     * Generate a token data array.
104
     *
105
     * @param  string  $key
106
     * @param  string|int|null  $time
107
     * @return array
108
     */
109
    public function generateDataForKey($key, $time = null)
110
    {
111
        if ($secret = $this->client->getAppSecretForKey($key)) {
112
            return $this->generateData($key, $secret, $time);
113
        }
114
115
        return [];
116
    }
117
118
    /**
119
     * Generate HTTP headers with a new token.
120
     *
121
     * @param  string  $appKey
122
     * @param  string|int|null  $time
123
     * @return array
124
     */
125
    public function generateHttpHeaders($appKey, $time = null)
126
    {
127
        $headers = [];
128
129
        foreach ($this->generateDataForKey($appKey, $time) as $key => $value) {
130
            $headers['X-API-'.strtoupper($key)] = $value;
131
        }
132
133
        return $headers;
134
    }
135
136
    /**
137
     * Generate query data with a new token.
138
     *
139
     * @param  string  $appKey
140
     * @param  string|int|null  $time
141
     * @return array
142
     */
143
    public function generateQueryData($appKey, $time = null)
144
    {
145
        $query = [];
146
147
        foreach ($this->generateDataForKey($appKey, $time) as $key => $value) {
148
            $query['_'.$key] = $value;
149
        }
150
151
        return $query;
152
    }
153
154
    /**
155
     * Generate query string with a new token.
156
     *
157
     * @param  string  $appKey
158
     * @param  string|int|null  $time
159
     * @return string
160
     */
161
    public function generateQueryString($appKey, $time = null)
162
    {
163
        return http_build_query($this->generateQueryData($appKey, $time));
164
    }
165
}
166