Completed
Push — master ( b88ec9...7d2baf )
by Patrick
02:59
created

APIManager::__construct()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 19
nc 12
nop 7
1
<?php
2
3
namespace Mailxpert\APIBundle\Util;
4
5
use Mailxpert\APIBundle\Exceptions\MailxpertAPIBundleException;
6
use Mailxpert\APIBundle\Model\AccessTokenInterface;
7
use Mailxpert\APIBundle\Model\AccessTokenManagerInterface;
8
use Mailxpert\Authentication\AccessToken as SDKAccessToken;
9
use Mailxpert\Mailxpert;
10
11
/**
12
 * Class APIManager
13
 * @package Mailxpert\APIBundle\Util
14
 */
15
class APIManager
16
{
17
    /**
18
     * @var string $appId mailXpert API Application Id
19
     */
20
    private $appId;
21
22
    /**
23
     * @var string $appSecret mailXpert API Application secret
24
     */
25
    private $appSecret;
26
27
    /**
28
     * @var string $redirectUrl mailXpert API Redirection URL
29
     */
30
    private $redirectUrl;
31
32
    /**
33
     * @var string $scope mailXpert OAuth Scope
34
     */
35
    private $scope;
36
37
    /**
38
     * @var Mailxpert
39
     */
40
    private $mailxpert;
41
42
    /**
43
     * @var AccessTokenManagerInterface
44
     */
45
    private $accessTokenManager;
46
47
    /**
48
     * AccessTokenManager constructor.
49
     *
50
     * @param AccessTokenManagerInterface $accessTokenManager
51
     * @param string                      $appId
52
     * @param string                      $appSecret
53
     * @param string                      $redirectUrl
54
     * @param string|null                 $scope
55
     * @param null                        $APIBaseUrl
56
     * @param null                        $APIOAuthUrl
57
     */
58
    public function __construct(AccessTokenManagerInterface $accessTokenManager, $appId, $appSecret, $redirectUrl, $scope = null, $APIBaseUrl = null, $APIOAuthUrl = null)
59
    {
60
        $this->appId = $appId;
61
        $this->appSecret = $appSecret;
62
        $this->redirectUrl = $redirectUrl;
63
        $this->scope = $scope;
64
        $this->accessTokenManager = $accessTokenManager;
65
66
        $config = [
67
            'app_id' => $this->appId,
68
            'app_secret' => $this->appSecret,
69
        ];
70
71
        if (!is_null($APIBaseUrl) && !empty($APIBaseUrl)) {
72
            $config['api_base_url'] = $APIBaseUrl;
73
        }
74
75
        if (!is_null($APIOAuthUrl) && !empty($APIOAuthUrl)) {
76
            $config['oauth_base_url'] = $APIOAuthUrl;
77
        }
78
79
        if ($accessTokenManager->hasAccessToken()) {
80
            $accessToken = $accessTokenManager->getAccessToken();
81
82
            if ($accessToken && $accessToken->isValid()) {
83
                $config['access_token'] = $accessToken->getSDKAccessToken();
84
            }
85
        }
86
87
        $this->mailxpert = new Mailxpert(
88
            $config
89
        );
90
    }
91
92
    /**
93
     * @return Mailxpert
94
     */
95
    public function getMailxpert()
96
    {
97
        return $this->mailxpert;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getLoginUrl()
104
    {
105
        $scope = explode(',', $this->scope);
106
107
        return $this->getMailxpert()->getLoginHelper()->getLoginUrl($this->redirectUrl, $scope);
108
    }
109
110
    /**
111
     * @return SDKAccessToken
112
     */
113
    public function retrieveAccessToken()
114
    {
115
        return $this->getMailxpert()->getLoginHelper()->getAccessToken($this->redirectUrl);
116
    }
117
118
    /**
119
     * @param SDKAccessToken $accessToken
120
     */
121
    public function setAccessToken(SDKAccessToken $accessToken)
122
    {
123
        $this->getMailxpert()->setAccessToken($accessToken);
124
    }
125
126
    /**
127
     * @param AccessTokenInterface|null $accessToken
128
     *
129
     * @return AccessTokenInterface
130
     * @throws MailxpertAPIBundleException
131
     */
132
    public function refreshAccessToken(AccessTokenInterface $accessToken = null)
133
    {
134
        if (is_null($accessToken)) {
135
            if ($this->getAccessTokenManager()->hasAccessToken()) {
136
                $accessToken = $this->getAccessTokenManager()->getAccessToken();
137
            }
138
        }
139
140
        if (!$accessToken) {
141
            throw new MailxpertAPIBundleException('No access token object provided or stored in session');
142
        }
143
144
        $SDKAccessToken = $this->getMailxpert()->getLoginHelper()->refreshAccessToken($accessToken->getSDKAccessToken(), $this->redirectUrl);
145
146
        return $this->getAccessTokenManager()->updateAccessToken($accessToken, $SDKAccessToken);
147
    }
148
149
    /**
150
     * @return AccessTokenManagerInterface
151
     */
152
    private function getAccessTokenManager()
153
    {
154
        return $this->accessTokenManager;
155
    }
156
157
158
}