Completed
Push — master ( 24e956...c25259 )
by Jared
02:44
created

Xero::userScreenName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Invoiced\OAuth1\Client\Server;
4
5
use Exception;
6
use GuzzleHttp\Client as GuzzleHttpClient;
7
use InvalidArgumentException;
8
use League\OAuth1\Client\Credentials\ClientCredentials;
9
use League\OAuth1\Client\Credentials\TokenCredentials;
10
use League\OAuth1\Client\Server\Server;
11
use League\OAuth1\Client\Signature\SignatureInterface;
12
13
class Xero extends Server
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $responseType = 'xml';
19
20
    /**
21
     * @var bool
22
     */
23
    protected $usePartnerApi = false;
24
25
    /**
26
     * @var array
27
     */
28
    protected $httpClientOptions = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $lastTokenCredentialsResponse;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function __construct($clientCredentials, SignatureInterface $signature = null)
39
    {
40
        if (is_array($clientCredentials)) {
41
            $this->parseConfiguration($clientCredentials);
42
43
            $clientCredentials = $this->createClientCredentials($clientCredentials);
44
45
            if (!$signature && $clientCredentials instanceof RsaClientCredentials) {
46
                $signature = new RsaSha1Signature($clientCredentials);
47
            }
48
        }
49
50
        parent::__construct($clientCredentials, $signature);
51
    }
52
53
    /**
54
     * Sets whether the Xero partner API should be used.
55
     *
56
     * @param bool $enable
57
     *
58
     * @return self
59
     */
60
    public function usePartnerApi($enable = true)
61
    {
62
        $this->usePartnerApi = $enable;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Checks if the Xero partner API is used.
69
     *
70
     * @return bool
71
     */
72
    public function getUsePartnerApi()
73
    {
74
        return $this->usePartnerApi;
75
    }
76
77
    /**
78
     * Creates a Guzzle HTTP client for the given URL.
79
     *
80
     * @return GuzzleHttpClient
81
     */
82
    public function createHttpClient()
83
    {
84
        return new GuzzleHttpClient($this->httpClientOptions);
85
    }
86
87
    public function urlTemporaryCredentials()
88
    {
89
        if ($this->usePartnerApi) {
90
            return 'https://api-partner.network.xero.com/oauth/RequestToken';
91
        }
92
93
        return 'https://api.xero.com/oauth/RequestToken';
94
    }
95
96
    public function urlAuthorization()
97
    {
98
        return 'https://api.xero.com/oauth/Authorize';
99
    }
100
101
    public function urlTokenCredentials()
102
    {
103
        if ($this->usePartnerApi) {
104
            return 'https://api-partner.network.xero.com/oauth/AccessToken';
105
        }
106
107
        return 'https://api.xero.com/oauth/AccessToken';
108
    }
109
110
    public function urlUserDetails()
111
    {
112
        return $this->notSupportedByXero();
113
    }
114
115
    public function userDetails($data, TokenCredentials $tokenCredentials)
116
    {
117
        return $this->notSupportedByXero();
118
    }
119
120
    public function userUid($data, TokenCredentials $tokenCredentials)
121
    {
122
        return $this->notSupportedByXero();
123
    }
124
125
    public function userEmail($data, TokenCredentials $tokenCredentials)
126
    {
127
        return $this->notSupportedByXero();
128
    }
129
130
    public function userScreenName($data, TokenCredentials $tokenCredentials)
131
    {
132
        return $this->notSupportedByXero();
133
    }
134
135
    /**
136
     * Gets the response of the last access token call. This might
137
     * be useful for partner applications to retrieve additional
138
     * OAuth parameters passed in by Xero.
139
     *
140
     * @return array|null
141
     */
142
    public function getLastTokenCredentialsResponse()
143
    {
144
        return $this->lastTokenCredentialsResponse;
145
    }
146
147
    protected function notSupportedByXero()
148
    {
149
        throw new Exception("Xero's API does not support retrieving the current user. Please see https://xero.uservoice.com/forums/5528-xero-accounting-api/suggestions/5688571-expose-which-user-connected-the-organization-via-o");
150
    }
151
152
    /**
153
     * Parse configuration array to set attributes.
154
     *
155
     * @param array $configuration
156
     */
157
    private function parseConfiguration(array $configuration = array())
158
    {
159
        $configToPropertyMap = array(
160
            'partner' => 'usePartnerApi',
161
            'http_client' => 'httpClientOptions',
162
        );
163
        foreach ($configToPropertyMap as $config => $property) {
164
            if (isset($configuration[$config])) {
165
                $this->$property = $configuration[$config];
166
            }
167
        }
168
    }
169
170
    /**
171
     * Creates a client credentials instance from an array of credentials.
172
     *
173
     * @param array $clientCredentials
174
     *
175
     * @return ClientCredentials
176
     */
177
    protected function createClientCredentials(array $clientCredentials)
178
    {
179
        $keys = array('identifier', 'secret');
180
181
        foreach ($keys as $key) {
182
            if (!isset($clientCredentials[$key])) {
183
                throw new InvalidArgumentException("Missing client credentials key [$key] from options.");
184
            }
185
        }
186
187
        if (isset($clientCredentials['rsa_private_key']) && isset($clientCredentials['rsa_public_key'])) {
188
            $_clientCredentials = new RsaClientCredentials();
189
            $_clientCredentials->setRsaPrivateKey($clientCredentials['rsa_private_key']);
190
            $_clientCredentials->setRsaPublicKey($clientCredentials['rsa_public_key']);
191
        } else {
192
            $_clientCredentials = new ClientCredentials();
193
        }
194
195
        $_clientCredentials->setIdentifier($clientCredentials['identifier']);
196
        $_clientCredentials->setSecret($clientCredentials['secret']);
197
198
        if (isset($clientCredentials['callback_uri'])) {
199
            $_clientCredentials->setCallbackUri($clientCredentials['callback_uri']);
200
        }
201
202
        return $_clientCredentials;
203
    }
204
205
    /**
206
     * Creates token credentials from the body response.
207
     *
208
     * @param string $body
209
     *
210
     * @return TokenCredentials
211
     */
212
    protected function createTokenCredentials($body)
213
    {
214
        parse_str($body, $data);
215
        $this->lastTokenCredentialsResponse = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can be null. However, the property $lastTokenCredentialsResponse is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
216
217
        return parent::createTokenCredentials($body);
218
    }
219
}
220