ExchangeWebServicesAuth::fromUsernameAndPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace garethp\ews\API;
4
5
use GuzzleHttp\Client;
6
7
class ExchangeWebServicesAuth
8
{
9 38
    public static function fromUsernameAndPassword($username, $password)
10
    {
11 38
        return array(
12 38
            'curl' => array(
13 38
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_NTLM,
14 38
                CURLOPT_USERPWD => $username.':'.$password
15 38
            )
16 38
        );
17
    }
18
19 1
    public static function fromCallbackToken($token)
20
    {
21 1
        return array(
22 1
            'headers' => array(
23 1
                'Authorization' => 'Bearer '.$token
24 1
            )
25 1
        );
26
    }
27
28
    public static function getTokenFromAuthorizationCode(
29
        $clientId,
30
        $clientSecret,
31
        $authorizationCode,
32
        $redirectUri,
33
        $tokenEndPoint = 'https://login.microsoftonline.com/common/oauth2/token'
34
    ) {
35
        $postOptions = array(
36
            'http_errors' => false,
37
            'form_params' => array(
38
                'client_id' => $clientId,
39
                'resource' => 'https://outlook.office365.com',
40
                'client_secret' => $clientSecret,
41
                'code' => $authorizationCode,
42
                'redirect_uri' => $redirectUri,
43
                'grant_type' => 'authorization_code'
44
            )
45
        );
46
47
        $client = new Client();
48
        $response = $client->request('POST', $tokenEndPoint, $postOptions);
49
        $response = $response->getBody()->__toString();
50
51
        $response = json_decode($response);
52
53
        return $response->access_token;
54
    }
55
}
56