Guard::getAccessToken()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 16
nc 3
nop 1
1
<?php
2
3
namespace App\Auth;
4
5
use App\MemberToken;
6
use App\Member;
7
use Facebook\FacebookAuthorizationException;
8
use Facebook\FacebookSession;
9
use Facebook\FacebookRequest;
10
use Facebook\GraphUser;
11
use Illuminate;
12
use Illuminate\Support\Facades\Config;
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Session;
15
use Illuminate\Http\Request as HttpRequest;
16
17
/**
18
 * Class Guard
19
 * @package App\Auth
20
 */
21
class Guard
22
{
23
    /**
24
     * @param $code
25
     * @return null
26
     * @throws \Facebook\FacebookRequestException
27
     * @throws \Facebook\FacebookSDKException
28
     */
29
    protected function getAccessToken($code)
30
    {
31
        $response = (new FacebookRequest(
32
            FacebookSession::newAppSession(),
33
            'GET',
34
            '/oauth/access_token',
35
            [
36
                'client_id' => FacebookSession::_getTargetAppId(),
37
                'client_secret' => FacebookSession::_getTargetAppSecret(),
38
                'redirect_uri' => Config::get('auth.providers.facebook.redirect_uri'),
39
                'code' => $code
40
            ]
41
        ))->execute()->getResponse();
42
43
        // Graph v2.3 and greater return objects on the /oauth/access_token endpoint
44
        $accessToken = null;
45
        if (is_object($response) && isset($response->access_token)) {
46
            $accessToken = $response->access_token;
47
        } elseif (is_array($response) && array_key_exists('access_token', $response)) {
48
            $accessToken = $response['access_token'];
49
        }
50
51
        return $accessToken;
52
    }
53
54
    /**
55
     * @name facebookAuth
56
     * @param $code
57
     * @return null
58
     * @throws \Facebook\FacebookSDKException
59
     * @throws \Facebook\FacebookRequestException
60
     * @throws FacebookAuthorizationException
61
     */
62
    public function facebookAuth($code)
63
    {
64
        $accessToken = $this->getAccessToken($code);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $accessToken is correct as $this->getAccessToken($code) (which targets App\Auth\Guard::getAccessToken()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
66
        $session = new FacebookSession($accessToken);
67
68
        if (!$session) {
69
            throw new FacebookAuthorizationException('Invalid code', ['Invalid code'], 401);
70
        }
71
72
        /**
73
         * @var GraphUser $userProfile
74
         */
75
        $userProfile = (new FacebookRequest(
76
            $session,
77
            'GET',
78
            '/me'
79
        ))->execute()->getGraphObject(GraphUser::className());
80
81
        $user = Member::firstOrNew(['facebookId' => $userProfile->getId()]);
82
        $user->name = $userProfile->getName();
83
        $user->save();
84
85
        Auth::login($user);
86
87
        $memberToken = new MemberToken();
88
        $memberToken->memberId = $user->id;
0 ignored issues
show
Documentation introduced by
The property memberId does not exist on object<App\MemberToken>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
89
        $memberToken->accessToken = $accessToken;
0 ignored issues
show
Documentation introduced by
The property accessToken does not exist on object<App\MemberToken>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        $memberToken->sessionId = Session::getId();
0 ignored issues
show
Documentation introduced by
The property sessionId does not exist on object<App\MemberToken>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
        $memberToken->save();
92
93
        return $accessToken;
94
    }
95
96
    public function logout()
97
    {
98
        // If we have an event dispatcher instance, we can fire off the logout event
99
        // so any further processing can be done. This allows the developer to be
100
        // listening for anytime a user signs out of this application manually.
101
102
        MemberToken::where(['sessionId' => Session::getId()])->delete();
103
    }
104
105
    /**
106
     * @name getSessionId
107
     * @param HttpRequest $request
108
     * @return mixed
109
     */
110
    public static function getSessionId(HttpRequest $request)
111
    {
112
        $accessToken = trim(
113
            preg_replace('/^(?:\s+)?Bearer\s/', '', $request->header('Authorization'))
114
        );
115
116
        // find session with token
117
        $memberToken = MemberToken::where('accessToken', $accessToken)->first();
118
119
        if ($memberToken && Session::isValidId($memberToken->sessionId)) {
120
            /**
121
             * @var MemberToken $memberToken
122
             */
123
            $memberToken->update([
124
                'updated_at' => $memberToken->freshTimestamp()
125
            ]);
126
            return $memberToken->sessionId;
0 ignored issues
show
Documentation introduced by
The property sessionId does not exist on object<App\MemberToken>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
        } else {
128
            return $request->cookies->get(Session::getName());
129
        }
130
    }
131
}
132