Completed
Push — master ( a80b44...c26644 )
by Elf
04:22
created

AppServiceProvider::setLocaleForCarbon()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use ElfSundae\Laravel\Support\Helper;
8
use Illuminate\Support\ServiceProvider;
9
10
class AppServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap any application services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        $this->setLocaleForCarbon();
20
    }
21
22
    /**
23
     * Set locale for Carbon.
24
     *
25
     * @return bool
26
     */
27
    protected function setLocaleForCarbon()
28
    {
29
        return Carbon::setLocale($this->app['config']->get('support.carbon_locale'), $this->app->getLocale());
0 ignored issues
show
Unused Code introduced by
The call to Carbon::setLocale() has too many arguments starting with $this->app->getLocale().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
    }
31
32
    /**
33
     * Register any application services.
34
     *
35
     * @return void
36
     */
37
    public function register()
38
    {
39
        array_map([$this->app, 'register'], $this->getServiceProviders());
40
41
        $this->modifyCurrentRequest();
42
    }
43
44
    /**
45
     * Get service providers to be registered.
46
     *
47
     * @return array
48
     */
49
    protected function getServiceProviders()
50
    {
51
        $providers = [];
52
53
        if (is_app('admin') || $this->app->runningInConsole()) {
54
            array_push(
55
                $providers,
56
                \ElfSundae\Laravel\Datatables\DatatablesServiceProvider::class,
57
                \Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class
58
            );
59
        }
60
61
        return $providers;
62
    }
63
64
    /**
65
     * Modify the current request.
66
     *
67
     * @return void
68
     */
69
    protected function modifyCurrentRequest()
70
    {
71
        Helper::addAcceptableJsonType(function (Request $request) {
72
            return $this->shouldAddAcceptableJsonType($request);
73
        });
74
75
        if ($this->shouldFakeAppClient()) {
76
            $this->fakeAppClient();
77
        }
78
    }
79
80
    /**
81
     * Determines appending JSON type to the "Accept" header for the current request.
82
     *
83
     * @param  \Illuminate\Http\Request  $request
84
     * @return bool
85
     */
86
    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...
87
    {
88
        return $this->isApiRequest();
89
    }
90
91
    /**
92
     * Indicates the current request is an API request, e.g. the request is sent
93
     * from an API client.
94
     *
95
     * @return bool
96
     */
97
    protected function isApiRequest()
98
    {
99
        return is_app('api');
100
    }
101
102
    /**
103
     * Determines making a fake API client for the current request.
104
     *
105
     * @return bool
106
     */
107
    protected function shouldFakeAppClient()
108
    {
109
        return $this->app['config']->get('app.debug') &&
110
            $this->app->isLocal() &&
111
            $this->app['request']->ip() === '127.0.0.1' &&
112
            $this->isApiRequest();
113
    }
114
115
    /**
116
     * Fake current agent client as an app client.
117
     *
118
     * @return void
119
     */
120
    protected function fakeAppClient()
121
    {
122
        $this->app->resolving('agent.client', function ($client) {
123
            if (! $client->is('AppClient')) {
124
                $client->setUserAgent($this->getUserAgentForFakeAppClient());
125
            }
126
        });
127
    }
128
129
    /**
130
     * Get User-Agent string for fake app client.
131
     *
132
     * @return string
133
     */
134
    protected function getUserAgentForFakeAppClient()
135
    {
136
        $clientData = urlsafe_base64_encode(json_encode(
137
            $this->app['config']->get('support.fake_app_client', [])
138
        ));
139
140
        $userAgent = $this->app['request']->header('User-Agent');
141
142
        return "$userAgent client({$clientData})";
143
    }
144
}
145