Test Setup Failed
Push — master ( f8e1e2...b86eb5 )
by Php Easy Api
04:07
created

AuthenticateToken::getTokenFromProvider()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Authenticate;
4
5
trait AuthenticateToken
6
{
7
    /**
8
     * @var null
9
     */
10
    public $credentialHash=null;
11
12
    /**
13
     * @return string
14
     */
15
    public function getTokenData()
16
    {
17
        // the absolute params property must be present
18
        // in the object and the params value must be the builder key.
19
        if(property_exists($this,'params') and isset($this->params['builder'])){
20
21
            // a real token will be generated after
22
            // you get the first method of the query builder value.
23
            $authData = $this->params['data'];
24
25
            // we refer to the token closure feature on the config to enable
26
            // the creation of user-based tokens on the application side.
27
            return $this->getTokenFromProvider($authData,function() use($authData){
28
                return md5(sha1($authData->id.'__'.$this->credentialHash.'__'.time().'__'.fingerPrint()));
29
            });
30
        }
31
32
        return null;
33
    }
34
35
    /**
36
     * @param $authData
37
     * @param callable $callback
38
     * @return mixed
39
     */
40
    private function getTokenFromProvider($authData,callable $callback)
41
    {
42
        // if the token value is a closure value,
43
        // we will run a user-based token closure.
44
        if(app()->has('authenticate.token') && is_callable($token = app()->get('authenticate.token'))){
45
            return $token($authData);
46
        }
47
48
        // if there is no closure object on the token side,
49
        // we use the token value directly generated by the kernel.
50
        return call_user_func($callback);
51
    }
52
53
}