Completed
Pull Request — master (#137)
by
unknown
16:19
created

FacebookSdk::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 2
b 0
f 1
nc 1
nop 3
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace AppBundle\Services;
4
5
use Facebook;
6
7
class FacebookSdk
8
{
9
    /**
10
     * @var Facebook\GraphNodes\GraphUser
11
     */
12
    private $userNode;
13
14
    /**
15
     * @var string
16
     */
17
    private $appId;
18
19
    /**
20
     * @var string
21
     */
22
    private $appSecret;
23
24
    /**
25
     * @var string
26
     */
27
    private $graphVersion;
28
29
    /**
30
     * @param string $appId
31
     * @param string $appSecret
32
     * @param string $graphVersion
33
     */
34
    public function __construct($appId, $appSecret, $graphVersion)
35
    {
36
        $this->appId = $appId;
37
        $this->appSecret = $appSecret;
38
        $this->graphVersion = $graphVersion;
39
    }
40
41
    /**
42
     * @param $accessToken
43
     * @return Facebook\GraphNodes\GraphUser
44
     * @throws \Exception
45
     */
46
    public function getUserFacebook($accessToken)
47
    {
48
        $fb = new Facebook\Facebook([
49
            'app_id' => $this->appId,
50
            'app_secret' => $this->appSecret,
51
            'default_graph_version' => $this->graphVersion,
52
        ]);
53
        $fb->setDefaultAccessToken($accessToken);
54
55
        try {
56
            $response = $fb->get('/me?fields=id,email,first_name,last_name');
57
            $this->userNode = $response->getGraphUser();
58
59
            return $this->userNode;
60
        } catch (Facebook\Exceptions\FacebookResponseException $e) {
61
            // When Graph returns an error
62
            throw new \Exception('Graph returned an error: '.$e->getMessage());
63
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
64
            // When validation fails or other local issues
65
            throw new \Exception('Facebook SDK returned an error: '.$e->getMessage());
66
        }
67
    }
68
}
69