Auth::getError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpEarth\Stats;
4
5
use Facebook\Facebook;
6
use Facebook\Exceptions\FacebookSDKException;
7
use Facebook\Exceptions\FacebookResponseException;
8
9
/**
10
 * Class Auth.
11
 */
12
class Auth
13
{
14
    /**
15
     * @var Facebook
16
     */
17
    public $fb;
18
19
    /**
20
     * @var Config
21
     */
22
    private $config;
23
24
    /**
25
     * @var
26
     */
27
    private $error;
28
29
    /**
30
     * Auth constructor.
31
     *
32
     * @param Config $config
33
     */
34
    public function __construct(Config $config)
35
    {
36
        $this->config = $config;
37
38
        $this->fb = new Facebook([
39
            'app_id' => $this->config->getParameter('fb_app_id'),
40
            'app_secret' => $this->config->getParameter('fb_app_secret'),
41
            'default_graph_version' => $this->config->getParameter('default_graph_version'),
42
        ]);
43
    }
44
45
    /**
46
     * Checks if current Facebook access token is valid.
47
     *
48
     * @return bool
49
     */
50
    public function isValid()
51
    {
52
        try {
53
            $this->fb->get('/me');
54
            $this->error = null;
55
56
            return true;
57
        } catch (FacebookResponseException $e) {
58
            if ($e->getErrorType() == 'OAuthException') {
59
                $this->error = $e->getMessage().' Error type: '.$e->getErrorType();
60
            } else {
61
                $this->error = $e->getMessage();
62
            }
63
64
            return false;
65
        } catch (FacebookSDKException $e) {
66
            $this->error = $e->getMessage();
67
68
            return false;
69
        }
70
    }
71
72
    /**
73
     * Get error.
74
     *
75
     * @return mixed
76
     */
77
    public function getError()
78
    {
79
        return $this->error;
80
    }
81
82
    /**
83
     * Set default Facebook access token.
84
     *
85
     * @param string $token
86
     */
87
    public function setToken($token)
88
    {
89
        $this->fb->setDefaultAccessToken($token);
90
    }
91
}
92