Completed
Push — master ( 8d39af...8d27ae )
by Jared
01:55
created

Xero   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 134
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A usePartnerApi() 0 6 1
A getUsePartnerApi() 0 4 1
A createHttpClient() 0 4 1
A urlTemporaryCredentials() 0 8 2
A urlAuthorization() 0 4 1
A urlTokenCredentials() 0 8 2
A urlUserDetails() 0 4 1
A userDetails() 0 4 1
A userUid() 0 4 1
A userEmail() 0 4 1
A userScreenName() 0 4 1
A notSupportedByXero() 0 4 1
A parseConfiguration() 0 12 3
1
<?php
2
3
namespace Invoiced\OAuth1\Client\Server;
4
5
use Exception;
6
use GuzzleHttp\Client as GuzzleHttpClient;
7
use League\OAuth1\Client\Credentials\TokenCredentials;
8
use League\OAuth1\Client\Server\Server;
9
use League\OAuth1\Client\Signature\SignatureInterface;
10
11
class Xero extends Server
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $responseType = 'xml';
17
18
    /**
19
     * @var bool
20
     */
21
    protected $usePartnerApi = false;
22
23
    /**
24
     * @var array
25
     */
26
    protected $httpClientOptions = [];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct($clientCredentials, SignatureInterface $signature = null)
32
    {
33
        parent::__construct($clientCredentials, $signature);
34
35
        if (is_array($clientCredentials)) {
36
            $this->parseConfiguration($clientCredentials);
37
        }
38
    }
39
40
    /**
41
     * Sets whether the Xero partner API should be used.
42
     *
43
     * @param bool $enable
44
     *
45
     * @return self
46
     */
47
    public function usePartnerApi($enable = true)
48
    {
49
        $this->usePartnerApi = $enable;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Checks if the Xero partner API is used.
56
     *
57
     * @return bool
58
     */
59
    public function getUsePartnerApi()
60
    {
61
        return $this->usePartnerApi;
62
    }
63
64
    /**
65
     * Creates a Guzzle HTTP client for the given URL.
66
     *
67
     * @return GuzzleHttpClient
68
     */
69
    public function createHttpClient()
70
    {
71
        return new GuzzleHttpClient($this->httpClientOptions);
72
    }
73
74
    public function urlTemporaryCredentials()
75
    {
76
        if ($this->usePartnerApi) {
77
            return 'https://api-partner.network.xero.com/oauth/RequestToken';
78
        }
79
80
        return 'https://api.xero.com/oauth/RequestToken';
81
    }
82
83
    public function urlAuthorization()
84
    {
85
        return 'https://api.xero.com/oauth/Authorize';
86
    }
87
88
    public function urlTokenCredentials()
89
    {
90
        if ($this->usePartnerApi) {
91
            return 'https://api-partner.network.xero.com/oauth/AccessToken';
92
        }
93
94
        return 'https://api.xero.com/oauth/AccessToken';
95
    }
96
97
    public function urlUserDetails()
98
    {
99
        return $this->notSupportedByXero();
100
    }
101
102
    public function userDetails($data, TokenCredentials $tokenCredentials)
103
    {
104
        return $this->notSupportedByXero();
105
    }
106
107
    public function userUid($data, TokenCredentials $tokenCredentials)
108
    {
109
        return $this->notSupportedByXero();
110
    }
111
112
    public function userEmail($data, TokenCredentials $tokenCredentials)
113
    {
114
        return $this->notSupportedByXero();
115
    }
116
117
    public function userScreenName($data, TokenCredentials $tokenCredentials)
118
    {
119
        return $this->notSupportedByXero();
120
    }
121
122
    protected function notSupportedByXero()
123
    {
124
        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");
125
    }
126
127
    /**
128
     * Parse configuration array to set attributes.
129
     *
130
     * @param array $configuration
131
     */
132
    private function parseConfiguration(array $configuration = array())
133
    {
134
        $configToPropertyMap = array(
135
            'partner' => 'usePartnerApi',
136
            'http_client' => 'httpClientOptions',
137
        );
138
        foreach ($configToPropertyMap as $config => $property) {
139
            if (isset($configuration[$config])) {
140
                $this->$property = $configuration[$config];
141
            }
142
        }
143
    }
144
}
145