Helper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 60
ccs 0
cts 23
cp 0
rs 10
c 5
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addAcceptableJsonTypeForRequest() 0 15 5
A setCurrentAppKeyForRequest() 0 7 2
A getCurrentAppKey() 0 5 2
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
use Closure;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
9
class Helper
10
{
11
    /**
12
     * Stores the app key of the current api client.
13
     */
14
    const CURRENT_APP_KEY = 'current_app_key';
15
16
    /**
17
     * Set the current app key for the request.
18
     *
19
     * @param  string  $key
20
     * @param  \Illuminate\Http\Request|null  $request
21
     * @return \Illuminate\Http\Request
22
     */
23
    public static function setCurrentAppKeyForRequest($key, Request $request = null)
24
    {
25
        $request = $request ?: app('request');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $request = $request ?: /** @scrutinizer ignore-call */ app('request');
Loading history...
26
27
        $request->attributes->set(static::CURRENT_APP_KEY, $key);
28
29
        return $request;
30
    }
31
32
    /**
33
     * Get the app key of the current api client.
34
     *
35
     * @param  \Illuminate\Http\Request|null  $request
36
     * @return string|null
37
     */
38
    public static function getCurrentAppKey(Request $request = null)
39
    {
40
        $request = $request ?: app('request');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $request = $request ?: /** @scrutinizer ignore-call */ app('request');
Loading history...
41
42
        return $request->attributes->get(static::CURRENT_APP_KEY);
43
    }
44
45
    /**
46
     * Add JSON type to the "Accept" header for the request.
47
     *
48
     * @see https://laravel-china.org/topics/3430
49
     *
50
     * @param  \Closure  $determination
51
     * @param  \Closure  $callback
52
     * @return mixed
53
     */
54
    public static function addAcceptableJsonTypeForRequest(Closure $determination = null, Closure $callback = null)
55
    {
56
        app()->rebinding('request', function ($app, Request $request) use ($determination, $callback) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        /** @scrutinizer ignore-call */ 
57
        app()->rebinding('request', function ($app, Request $request) use ($determination, $callback) {
Loading history...
57
            if (is_null($determination) || $determination($request)) {
58
                $accept = $request->headers->get('Accept');
59
60
                if (! Str::contains($accept, ['/json', '+json'])) {
61
                    $accept = rtrim('application/json,'.$accept, ',');
62
63
                    $request->headers->set('Accept', $accept);
64
                    $request->server->set('HTTP_ACCEPT', $accept);
65
                    $_SERVER['HTTP_ACCEPT'] = $accept;
66
67
                    if ($callback) {
68
                        $callback($request);
69
                    }
70
                }
71
            }
72
        });
73
    }
74
}
75