Completed
Push — master ( 3bda43...8b01ef )
by Elf
03:34
created

AppServiceProvider::isApiRequest()   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 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Providers;
4
5
use Illuminate\Http\Request;
6
use ElfSundae\Laravel\Support\Helper;
7
use Illuminate\Support\ServiceProvider;
8
9
class AppServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap any application services.
13
     *
14
     * @return void
15
     */
16
    public function boot()
17
    {
18
    }
19
20
    /**
21
     * Register any application services.
22
     *
23
     * @return void
24
     */
25
    public function register()
26
    {
27
        array_map([$this->app, 'register'], $this->getServiceProviders());
28
29
        $this->modifyCurrentRequest();
30
    }
31
32
    /**
33
     * Get service providers to be registered.
34
     *
35
     * @return array
36
     */
37
    protected function getServiceProviders()
38
    {
39
        $providers = [];
40
41
        if (is_app('admin') || $this->app->runningInConsole()) {
42
            array_push(
43
                $providers,
44
                \ElfSundae\Laravel\Datatables\DatatablesServiceProvider::class,
45
                \Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class
46
            );
47
        }
48
49
        return $providers;
50
    }
51
52
    /**
53
     * Modify the current request.
54
     *
55
     * @return void
56
     */
57
    protected function modifyCurrentRequest()
58
    {
59
        Helper::addAcceptableJsonType(function (Request $request) {
60
            return $this->shouldAddAcceptableJsonType($request);
61
        });
62
63
        if ($this->shouldFakeAppClient()) {
64
            $this->fakeAppClient();
65
        }
66
    }
67
68
    /**
69
     * Determines appending JSON type to the "Accept" header for the current request.
70
     *
71
     * @param  \Illuminate\Http\Request  $request
72
     * @return bool
73
     */
74
    protected function shouldAddAcceptableJsonType(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        return $this->isApiRequest();
77
    }
78
79
    /**
80
     * Indicates the current request is an API request, e.g. the request is sent
81
     * from an API client.
82
     *
83
     * @return bool
84
     */
85
    protected function isApiRequest()
86
    {
87
        return is_app('api');
88
    }
89
90
    /**
91
     * Determines making a fake API client for the current request.
92
     *
93
     * @return bool
94
     */
95
    protected function shouldFakeAppClient()
96
    {
97
        return config('app.debug') &&
98
            $this->app->isLocal() &&
99
            $this->app['request']->ip() === '127.0.0.1' &&
100
            $this->isApiRequest();
101
    }
102
103
    /**
104
     * Fake current agent client as an app client.
105
     *
106
     * @return void
107
     */
108
    protected function fakeAppClient()
109
    {
110
        $this->app->resolving('agent.client', function ($client) {
111
            if (! $client->is('AppClient')) {
112
                $client->setUserAgent($this->getUserAgentForFakingAppClient());
113
            }
114
        });
115
116
        $this->app->rebinding('request', function ($app, $request) {
117
            if ($request->hasHeader('X-API-TOKEN') || $request->has('_token')) {
118
                return;
119
            }
120
121
            $request->headers->add(
122
                $this->getApiTokenHeadersForFakingAppClient()
123
            );
124
        });
125
    }
126
127
    /**
128
     * Get User-Agent string for faking app client.
129
     *
130
     * @return string
131
     */
132
    protected function getUserAgentForFakingAppClient()
133
    {
134
        $clientData = urlsafe_base64_encode(json_encode(
135
            config('support.fake_app_client', [])
136
        ));
137
138
        $userAgent = $this->app['request']->header('User-Agent');
139
140
        return "$userAgent client({$clientData})";
141
    }
142
143
    /**
144
     * Get API token request headers for faking app client.
145
     *
146
     * @return array
147
     */
148
    protected function getApiTokenHeadersForFakingAppClient()
149
    {
150
        $data = app('api.token')->generateDataForKey(
151
            app('api.client')->defaultAppKey()
152
        );
153
154
        $headers = [];
155
156
        foreach ($data as $key => $value) {
157
            $headers['X-API-'.strtoupper($key)] = $value;
158
        }
159
160
        return $headers;
161
    }
162
}
163