MailxpertLoginHelper::getAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sources.
4
 * Date: 14/08/15
5
 */
6
7
namespace Mailxpert\Helpers;
8
9
use Mailxpert\Authentication\AccessToken;
10
use Mailxpert\Authentication\OAuth2Client;
11
12
/**
13
 * Class MailxpertLoginHelper
14
 * @package Mailxpert\Helpers
15
 */
16
class MailxpertLoginHelper
17
{
18
    /**
19
     * @var OAuth2Client
20
     */
21
    private $oAuth2Client;
22
23
    /**
24
     * MailxpertRedirectLoginHelper constructor.
25
     *
26
     * @param OAuth2Client $oAuth2Client
27
     */
28
    public function __construct(OAuth2Client $oAuth2Client)
29
    {
30
        $this->oAuth2Client = $oAuth2Client;
31
    }
32
33
    /**
34
     * @param string $redirectUrl
35
     * @param array  $scope
36
     * @param string $separator
37
     *
38
     * @return string
39
     */
40
    public function getLoginUrl($redirectUrl, array $scope = [], $separator = '&')
41
    {
42
        return $this->makeUrl($redirectUrl, $scope, [], $separator);
43
    }
44
45
    /**
46
     * @param string $redirectUrl
47
     *
48
     * @return \Mailxpert\Authentication\AccessToken
49
     */
50
    public function getAccessToken($redirectUrl)
51
    {
52
        // TODO: CSRF
53
54
        return $this->oAuth2Client->getAccessTokenFromCode($this->getCode(), $redirectUrl);
55
    }
56
57
    /**
58
     * @param AccessToken $accessToken
59
     * @param string      $redirectUrl
60
     *
61
     * @return AccessToken
62
     */
63
    public function refreshAccessToken(AccessToken $accessToken, $redirectUrl)
64
    {
65
        return $this->oAuth2Client->getAccessTokenFromAccessToken($accessToken, $redirectUrl);
66
    }
67
68
    /**
69
     * Return the code.
70
     *
71
     * @return string|null
72
     */
73
    protected function getCode()
74
    {
75
        return $this->getInput('code');
76
    }
77
78
    /**
79
     * @param string $redirectUrl
80
     * @param array  $scope
81
     * @param array  $params
82
     * @param string $separator
83
     *
84
     * @return string
85
     */
86
    private function makeUrl($redirectUrl, array $scope, array $params, $separator = '&')
87
    {
88
        $state = null; // TODO: CSRF
89
90
        return $this->oAuth2Client->getAuthorizationUrl($redirectUrl, $state, $scope, $params, $separator);
91
    }
92
93
    /**
94
     * Returns a value from a GET param.
95
     *
96
     * @param string $key
97
     *
98
     * @return string|null
99
     */
100
    private function getInput($key)
101
    {
102
        return isset($_GET[$key]) ? $_GET[$key] : null;
103
    }
104
}
105