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

Helper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 84
ccs 0
cts 33
cp 0
rs 10
wmc 9
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setCurrentAppKeyForRequest() 0 6 1
A getCurrentAppKey() 0 4 1
A generateApiTokenForHttpHeaders() 12 12 3
A generateApiTokenForUrlQuery() 12 12 3
A generateApiTokenForUrlQueryString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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