Test Setup Failed
Push — master ( ba8aba...70cbb0 )
by Philippe
10:29
created

ClientUtility::injectAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This program is free software: you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation, either version 3 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Filoucrackeur\Hubic\Utility;
17
18
use Filoucrackeur\Hubic\Domain\Model\Account;
19
use Filoucrackeur\Hubic\Service\OAuth2\Client;
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
25
26
class ClientUtility implements SingletonInterface
27
{
28
    const AUTHORIZATION_ENDPOINT = 'https://api.hubic.com/oauth/auth/';
29
30
    const TOKEN_ENDPOINT = 'https://api.hubic.com/oauth/token/';
31
32
    const DOMAIN_API = 'https://api.hubic.com/';
33
34
    const VERSION_API = '1.0';
35
36
    /**
37
     * @var \Filoucrackeur\Hubic\Service\OAuth2\Client
38
     */
39
    protected $OAuth;
40
41
    /**
42
     * @var  \TYPO3\CMS\Extbase\Object\ObjectManager
43
     */
44
    protected $objectManager;
45
46
    /**
47
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
48
     */
49
    protected $persistenceManager;
50
51
    /**
52
     * @var \Filoucrackeur\Hubic\Domain\Model\Account
53
     */
54
    protected $account;
55
56
57
58
    /**
59
     * @param ObjectManager $objectManager
60
     */
61
    public function injectAccount(ObjectManager $objectManager) {
62
        $this->objectManager = $objectManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $objectManager of type object<Filoucrackeur\Hubic\Utility\ObjectManager> is incompatible with the declared type object<TYPO3\CMS\Extbase\Object\ObjectManager> of property $objectManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
    }
64
    
65
    /**
66
     * @param PersistenceManager $persistenceManager
67
     */
68
    public function injectPersistenceManager(PersistenceManager $persistenceManager) {
69
        $this->persistenceManager = $persistenceManager;
70
    }
71
72
    /**
73
     * @param Account $account
74
     */
75
    public function injectAccount(Account $account) {
76
        $this->account = $account;
77
    }
78
79
    /**
80
     * @param Account $account
81
     * @return bool
82
     */
83
    public function callHubic(Account $account) : bool
84
    {
85
        $this->account = $account;
86
        $this->OAuth = new Client($this->account->getClientId(), $this->account->getClientSecret());
87
        $this->OAuth->setScope('usage.r,account.r,getAllLinks.r,credentials.r,sponsorCode.r,activate.w,sponsored.r,links.drw');
88
        $this->OAuth->setAccessTokenType(1);
89
90
        if ($this->account->getAccessToken()) {
91
            $this->OAuth->setAccessToken($this->account->getAccessToken());
92
        } else {
93
            if (isset($_GET['formToken'])) {
94
                $code = str_replace('code=', '', strstr(GeneralUtility::_GET('formToken'), "code="));
95
            }
96
            if (isset($code)) {
97
                $params = array('code' => $code, 'redirect_uri' => $this->getRedirectUri($this->account));
98
                $response = $this->OAuth->getAccessToken(self::TOKEN_ENDPOINT, 'authorization_code', $params);
99
100
                if( $response ){
0 ignored issues
show
Bug Best Practice introduced by
The expression $response of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
                    if( $response['code'] == 200 ){
102
                        $this->account->setAccessToken($response['result']['access_token']);
103
                        $this->persistenceManager->update($this->account);
104
                        $this->persistenceManager->persistAll();
105
                        return true;
106
                    } else {
107
                        return false;
108
                    }
109
                } else {
110
                    return false;
111
                }
112
            }
113
        }
114
        return false;
115
    }
116
117
    public function getAccount()
118
    {
119
        $response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account');
120
        return $response;
121
    }
122
123
    /**
124
     * @param Account $account
125
     * @return string
126
     */
127
    public function getRedirectUri(Account $account)
128
    {
129
130
        $formProtection = FormProtectionFactory::get();
131
        $formToken = $formProtection->generateToken('AuthorizationRequest');
132
133
        return 'http://' . $_SERVER['HTTP_HOST'] . BackendUtility::getModuleUrl('tools_HubicHubic', [
134
            'tx_hubic_tools_hubichubic' => [
135
                'action' => 'authenticationResponse',
136
                'controller' => 'Backend\Account',
137
                'account' => $account->getUid()
138
            ],
139
            'formToken' => $formToken
140
        ]);
141
142
    }
143
144
    /**
145
     * @param Account $account
146
     * @return string
147
     */
148
    public function getAuthorizationRequestUrl(Account $account)
149
    {
150
        $this->callHubic($account);
151
        $authUrl = $this->OAuth->getAuthenticationUrl(self::AUTHORIZATION_ENDPOINT, $this->getRedirectUri($account));
152
        return $authUrl;
153
    }
154
155
    /**
156
     * Get hubiC account Quota
157
     *
158
     * @see https://api.hubic.com/console/
159
     * @return array
160
     */
161
    public function getAccountQuota()
162
    {
163
        $response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/usage');
164
        return $response;
165
    }
166
167
    /**
168
     * Get hubiC agreements
169
     *
170
     * @see https://api.hubic.com/console/
171
     * @return array
172
     */
173
    public function getAgreement()
174
    {
175
        $response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/agreement');
176
        return $response;
177
    }
178
179
    /**
180
     * Get hubiC getAllLinks
181
     *
182
     * @see https://api.hubic.com/console/
183
     * @return array
184
     */
185
    public function getAllLinks()
186
    {
187
        $response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/getAllLinks');
188
        return $response;
189
    }
190
191
    /**
192
     * @param Account $account
193
     */
194
    public function setAccount(Account $account)
195
    {
196
        $this->account = $account;
197
    }
198
199
}
200