Completed
Push — master ( 1a1372...bba840 )
by Elf
03:43
created

Helper::generateApiTokenForUrlQueryString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
use Illuminate\Http\Request;
6
7
class Helper
8
{
9
    /**
10
     * Stores the app key of current api client.
11
     */
12
    const CURRENT_APP_KEY = 'current_app_key';
13
14
    /**
15
     * Set current app key for the request.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @param  string  $key
19
     * @return \Illuminate\Http\Request
20
     */
21
    public static function setCurrentAppKeyForRequest(Request $request, $key)
22
    {
23
        $request->attributes->set(static::CURRENT_APP_KEY, $key);
24
25
        return $request;
26
    }
27
28
    /**
29
     * Get the app key of current api client.
30
     *
31
     * @param  \Illuminate\Http\Request  $request
32
     * @return string|null
33
     */
34
    public static function getCurrentAppKey(Request $request)
35
    {
36
        return $request->attributes->get(static::CURRENT_APP_KEY);
37
    }
38
39
    /**
40
     * Generate data of api token for HTTP headers.
41
     *
42
     * @param  string  $appKey
43
     * @param  string|int|null  $time
44
     * @return array
45
     */
46 View Code Duplication
    public static function generateApiTokenForHttpHeaders($appKey, $time = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $headers = [];
49
50
        if ($data = app(Token::class)->generateDataForKey($appKey, $time)) {
51
            foreach ($data as $key => $value) {
52
                $headers['X-API-'.strtoupper($key)] = $value;
53
            }
54
        }
55
56
        return $headers;
57
    }
58
59
    /**
60
     * Generate data of api token for URL query.
61
     *
62
     * @param  string  $appKey
63
     * @param  string|int|null  $time
64
     * @return array
65
     */
66 View Code Duplication
    public static function generateApiTokenForUrlQuery($appKey, $time = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $query = [];
69
70
        if ($data = app(Token::class)->generateDataForKey($appKey, $time)) {
71
            foreach ($data as $key => $value) {
72
                $query['_'.$key] = $value;
73
            }
74
        }
75
76
        return $query;
77
    }
78
79
    /**
80
     * Generate data of api token for URL query string.
81
     *
82
     * @param  string  $appKey
83
     * @param  string|int|null  $time
84
     * @return string
85
     */
86
    public static function generateApiTokenForUrlQueryString($appKey, $time = null)
87
    {
88
        return http_build_query(static::generateApiTokenForUrlQuery($appKey, $time));
89
    }
90
}
91