FieldProvider   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 1

Importance

Changes 0
Metric Value
wmc 32
lcom 4
cbo 1
dl 0
loc 298
rs 9.6
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setSessionStore() 0 4 1
A setRequest() 0 4 1
A setEnvironment() 0 4 1
A extend() 0 4 1
A setTokenProvider() 0 4 1
A __call() 0 9 2
A getSessionId() 0 14 2
A getUrl() 0 18 3
A getRequestMethod() 0 4 1
A getServerIp() 0 4 1
A getClientIp() 0 4 1
A getClientUserAgent() 0 4 1
A getEnvironment() 0 4 1
A getFromSession() 0 9 2
D getUserId() 0 31 10
A getProcessIdentifier() 0 4 1
1
<?php namespace Understand\UnderstandLumen;
2
3
use Understand\UnderstandLumen\TokenProvider;
4
use \Illuminate\Session\Store AS SessionStore;
5
use Illuminate\Http\Request;
6
7
class FieldProvider
8
{
9
10
    /**
11
     * The registered field providers.
12
     *
13
     * @var array
14
     */
15
    protected $providers = [];
16
17
    /**
18
     * Default field
19
     *
20
     * @var array
21
     */
22
    protected $defaultProviders = [
23
        'getSessionId',
24
        'getUrl',
25
        'getRequestMethod',
26
        'getServerIp',
27
        'getClientIp',
28
        'getClientUserAgent',
29
        'getEnvironment',
30
        'getFromSession',
31
        'getProcessIdentifier',
32
        'getUserId'
33
    ];
34
35
    /**
36
     * Session store
37
     *
38
     * @var \Illuminate\Session\Store
39
     */
40
    protected $session;
41
42
    /**
43
     * Server variable
44
     *
45
     * @var Request
46
     */
47
    protected $request;
48
49
    /**
50
     * Token provider
51
     *
52
     * @var TokenProvider
53
     */
54
    protected $tokenProvider;
55
56
    /**
57
     * Current environment
58
     *
59
     * @var string
60
     */
61
    protected $environment;
62
63
    /**
64
     * Create field provider instance and set default providers to provider list
65
     *
66
     * @param type $app
0 ignored issues
show
Bug introduced by
There is no parameter named $app. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
68
     */
69
    public function __construct()
70
    {
71
        foreach ($this->defaultProviders as $defaultProviderName)
72
        {
73
            $this->extend($defaultProviderName, [$this, $defaultProviderName]);
74
        }
75
    }
76
77
    /**
78
     * Set session store
79
     *
80
     * @param type $service
81
     */
82
    public function setSessionStore(SessionStore $service)
83
    {
84
        $this->session = $service;
85
    }
86
87
    /**
88
     * Set request
89
     *
90
     * @param Request $request
91
     */
92
    public function setRequest(Request $request)
93
    {
94
        $this->request = $request;
95
    }
96
97
    /**
98
     * Set current environment
99
     *
100
     * @param string $environment
101
     */
102
    public function setEnvironment($environment)
103
    {
104
        $this->environment = $environment;
105
    }
106
107
    /**
108
     * Register a custom HTML macro.
109
     *
110
     * @param string $name
111
     * @param  mixed  $provider
112
     * @return void
113
     */
114
    public function extend($name, $provider)
115
    {
116
        $this->providers[$name] = $provider;
117
    }
118
119
    /**
120
     * Set token provider
121
     *
122
     * @param TokenProvider $tokenProvider
123
     */
124
    public function setTokenProvider(TokenProvider $tokenProvider)
125
    {
126
        $this->tokenProvider = $tokenProvider;
127
    }
128
129
    /**
130
     * Handle class calls
131
     *
132
     * @param string $name
133
     * @param  mixed $params
134
     * @return mixed
135
     *
136
     * @throws \BadMethodCallException
137
     */
138
    public function __call($name, $params)
139
    {
140
        if (isset($this->providers[$name]))
141
        {
142
            return call_user_func_array($this->providers[$name], $params);
143
        }
144
145
        throw new \BadMethodCallException("Method {$name} does not exist.");
146
    }
147
148
    /**
149
     * Return hashed version of session id
150
     *
151
     * @return string
152
     */
153
    protected function getSessionId()
154
    {
155
        if ( ! $this->session)
156
        {
157
            return null;
158
        }
159
160
        $sessionId = $this->session->getId();
161
162
        // by default we provide only hashed version of session id
163
        $hashed = sha1($sessionId);
164
165
        return $hashed;
166
    }
167
168
    /**
169
     * Return current url
170
     *
171
     * @return string
172
     */
173
    protected function getUrl()
174
    {
175
        $url = $this->request->path();
176
177
        if ( ! starts_with($url, '/'))
178
        {
179
            $url = '/' . $url;
180
        }
181
182
        $queryString = $this->request->getQueryString();
183
184
        if ($queryString)
185
        {
186
            $url .= '?' . $queryString;
187
        }
188
189
        return $url;
190
    }
191
192
    /**
193
     * Return request method
194
     *
195
     * @return string
196
     */
197
    protected function getRequestMethod()
198
    {
199
        return $this->request->method();
200
    }
201
202
    /**
203
     * Return server ip address
204
     *
205
     * @return string
206
     */
207
    protected function getServerIp()
208
    {
209
        return $this->request->server->get('SERVER_ADDR');
210
    }
211
212
    /**
213
     * Return client ip
214
     *
215
     * @return string
216
     */
217
    protected function getClientIp()
218
    {
219
        return $this->request->getClientIp();
220
    }
221
222
    /**
223
     * Return client user agent string
224
     *
225
     * @return string
226
     */
227
    protected function getClientUserAgent()
228
    {
229
        return $this->request->server->get('HTTP_USER_AGENT');
230
    }
231
232
    /**
233
     * Return current enviroment
234
     *
235
     * @return string
236
     */
237
    protected function getEnvironment()
238
    {
239
        return $this->environment;
240
    }
241
242
    /**
243
     * Retrive parameter from current session
244
     *
245
     * @param string $key
246
     * @return string
247
     */
248
    protected function getFromSession($key)
249
    {
250
        if ( ! $this->session)
251
        {
252
            return null;
253
        }
254
255
        return $this->session->get($key);
256
    }
257
258
    /**
259
     * Return current active user id
260
     *
261
     * @return int
262
     */
263
    protected function getUserId()
264
    {
265
        try
266
        {
267
            if (class_exists('\Auth') && ($userId = \Auth::id()))
268
            {
269
                return $userId;
270
            }
271
        }
272
        catch (\Exception $e)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
273
        {}
274
        try
275
        {
276
            if (class_exists('\Sentinel') && ($user = \Sentinel::getUser()))
277
            {
278
                return $user->id;
279
            }
280
        }
281
        catch (\Exception $e)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
282
        {}
283
284
        try
285
        {
286
            if (class_exists('\Sentry') && ($user = \Sentry::getUser()))
287
            {
288
                return $user->id;
289
            }
290
        }
291
        catch (\Exception $e)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
292
        {}
293
    }
294
295
    /**
296
     * Return process identifier token
297
     *
298
     * @return string
299
     */
300
    protected function getProcessIdentifier()
301
    {
302
        return $this->tokenProvider->getToken();
303
    }
304
}