Completed
Push — master ( 7113c4...8875d3 )
by Yassine
14s
created

SignedRequestFromInputHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 84.38%

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 27
cts 32
cp 0.8438
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiateSignedRequest() 0 9 3
A __construct() 0 6 1
B getAccessToken() 0 16 5
A getRawSignedRequestFromPost() 0 7 2
A getRawSignedRequestFromCookie() 0 7 2
A getSignedRequest() 0 3 1
A getUserId() 0 3 2
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\Helper;
24
25
use Facebook\Facebook;
26
use Facebook\Application;
27
use Facebook\Client;
28
use Facebook\SignedRequest;
29
use Facebook\Authentication\AccessToken;
30
use Facebook\Authentication\OAuth2Client;
31
32
/**
33
 * @package Facebook
34
 */
35
abstract class SignedRequestFromInputHelper
36
{
37
    /**
38
     * @var null|SignedRequest the SignedRequest entity
39
     */
40
    protected $signedRequest;
41
42
    /**
43
     * @var Application the Application entity
44
     */
45
    protected $app;
46
47
    /**
48
     * @var OAuth2Client The OAuth 2.0 client service.
49
     */
50
    protected $oAuth2Client;
51
52
    /**
53
     * Initialize the helper and process available signed request data.
54
     *
55
     * @param Application $app          the Application entity
56
     * @param Client      $client       the client to make HTTP requests
57
     * @param string      $graphVersion the version of Graph to use
58
     */
59 8
    public function __construct(Application $app, Client $client, $graphVersion)
60
    {
61 8
        $this->app = $app;
62 8
        $this->oAuth2Client = new OAuth2Client($this->app, $client, $graphVersion);
63
64 8
        $this->instantiateSignedRequest();
65 8
    }
66
67
    /**
68
     * Instantiates a new SignedRequest entity.
69
     *
70
     * @param null|string
71
     * @param null|mixed $rawSignedRequest
72
     */
73 8
    public function instantiateSignedRequest($rawSignedRequest = null)
74
    {
75 8
        $rawSignedRequest = $rawSignedRequest ?: $this->getRawSignedRequest();
76
77 8
        if (!$rawSignedRequest) {
78 6
            return;
79
        }
80
81 5
        $this->signedRequest = new SignedRequest($this->app, $rawSignedRequest);
82 5
    }
83
84
    /**
85
     * Returns an AccessToken entity from the signed request.
86
     *
87
     * @throws \Facebook\Exception\SDKException
88
     *
89
     * @return null|AccessToken
90
     */
91 3
    public function getAccessToken()
92
    {
93 3
        if ($this->signedRequest && $this->signedRequest->hasOAuthData()) {
94 2
            $code = $this->signedRequest->get('code');
95 2
            $accessToken = $this->signedRequest->get('oauth_token');
96
97 2
            if ($code && !$accessToken) {
98 1
                return $this->oAuth2Client->getAccessTokenFromCode($code);
99
            }
100
101 1
            $expiresAt = $this->signedRequest->get('expires', 0);
102
103 1
            return new AccessToken($accessToken, $expiresAt);
104
        }
105
106 1
        return null;
107
    }
108
109
    /**
110
     * Returns the SignedRequest entity.
111
     *
112
     * @return null|SignedRequest
113
     */
114
    public function getSignedRequest()
115
    {
116
        return $this->signedRequest;
117
    }
118
119
    /**
120
     * Returns the user_id if available.
121
     *
122
     * @return null|string
123
     */
124
    public function getUserId()
125
    {
126
        return $this->signedRequest ? $this->signedRequest->getUserId() : null;
127
    }
128
129
    /**
130
     * Get raw signed request from input.
131
     *
132
     * @return null|string
133
     */
134
    abstract public function getRawSignedRequest();
135
136
    /**
137
     * Get raw signed request from POST input.
138
     *
139
     * @return null|string
140
     */
141 3
    public function getRawSignedRequestFromPost()
142
    {
143 3
        if (isset($_POST['signed_request'])) {
144 3
            return $_POST['signed_request'];
145
        }
146
147 1
        return null;
148
    }
149
150
    /**
151
     * Get raw signed request from cookie set from the Javascript SDK.
152
     *
153
     * @return null|string
154
     */
155 2
    public function getRawSignedRequestFromCookie()
156
    {
157 2
        if (isset($_COOKIE['fbsr_' . $this->app->getId()])) {
158 2
            return $_COOKIE['fbsr_' . $this->app->getId()];
159
        }
160
161
        return null;
162
    }
163
}
164