FacebookProvider   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 0
loc 128
wmc 14
lcom 1
cbo 7
ccs 0
cts 59
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 6
A getIdentifier() 0 4 1
B validate() 0 30 4
A decodeBody() 0 19 3
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\OAuth2\GrantType\ThirdParty\Provider;
9
10
use Zend\Http\Client;
11
use Zend\Http\Response;
12
use Zend\Json\Json;
13
use Zend\Stdlib\ArrayUtils;
14
15
class FacebookProvider implements
16
    ProviderInterface
17
{
18
    use ProviderTrait;
19
20
    /**
21
     * @var string
22
     */
23
    protected $uri;
24
25
    /**
26
     * @var string
27
     */
28
    protected $appId;
29
30
    /**
31
     * @var array
32
     */
33
    protected $clientOptions;
34
35
    /**
36
     * @var array
37
     */
38
    protected $endpointParams = [];
39
40
    /**
41
     * @param $options
42
     *
43
     * @throws Exception\InvalidArgumentException
44
     */
45
    public function __construct($options)
46
    {
47
        if (! isset($options['app_id'])) {
48
            throw new Exception\InvalidArgumentException('Missing "app_id" option');
49
        }
50
51
        $this->appId = $options['app_id'];
52
53
        if (! isset($options['uri'])) {
54
            throw new Exception\InvalidArgumentException('Missing "uri" option');
55
        }
56
57
        $this->uri = rtrim($options['uri'], '/');
58
59
        if (isset($options['client_options'])) {
60
            $this->clientOptions = $options['client_options'];
61
        }
62
63
        if (isset($options['endpoint_params']) && is_array($options['endpoint_params'])) {
64
            $this->endpointParams = $options['endpoint_params'];
65
        }
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getIdentifier()
72
    {
73
        return 'facebook';
74
    }
75
76
    /**
77
     * @param $userId
78
     * @param $accessToken
79
     * @param null $errorMessage
80
     *
81
     * @throws Exception\ClientException
82
     *
83
     * @return bool
84
     */
85
    public function validate($userId, $accessToken, &$errorMessage = null)
86
    {
87
        $client = new Client($this->uri . '/me', $this->clientOptions);
88
        $client->setMethod('GET');
89
        $params = ArrayUtils::merge($this->endpointParams, ['access_token' => $accessToken]);
90
        $client->setParameterGet($params);
91
92
        $userData = $this->decodeBody($client->send());
93
94
        if (isset($userData->error)) {
95
            throw new Exception\ClientException($userData->error->message, 400);
96
        }
97
98
        if (! isset($userData->id)) {
99
            throw new Exception\ClientException('Invalid data returned by provider', 400);
100
        }
101
102
        if ($userData->id !== $userId) {
103
            $errorMessage = 'user_id mismatch';
104
105
            return false;
106
        }
107
108
        $this->userId = $userData->id;
109
        unset($userData->id);
110
111
        $this->userData = (array) $userData;
112
113
        return true;
114
    }
115
116
    /**
117
     * @param Response $response
118
     *
119
     * @throws Exception\ClientException
120
     *
121
     * @return mixed
122
     */
123
    protected function decodeBody(Response $response)
124
    {
125
        if ($response->isServerError()) {
126
            throw new Exception\ClientException(sprintf(
127
                "'%s' provider encountered a '%s' error while querying '%s'",
128
                $this->getIdentifier(),
129
                $response->getReasonPhrase(),
130
                $this->uri
131
            ));
132
        }
133
134
        $body = Json::decode($response->getBody());
135
136
        if ($response->isClientError()) {
137
            throw new Exception\ClientException($body->error->message, $response->getStatusCode());
138
        }
139
140
        return $body;
141
    }
142
}
143