Completed
Push — master ( a90e83...cac1c4 )
by Jean C.
02:38
created

Moip::accounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip;
4
5
use Moip\Resource\Customer;
6
use Moip\Resource\Account;
7
use Moip\Resource\Entry;
8
use Moip\Resource\Multiorders;
9
use Moip\Resource\Orders;
10
use Moip\Resource\Payment;
11
use Moip\Resource\Transfers;
12
use Requests_Session;
13
14
/**
15
 * Class Moip.
16
 */
17
class Moip
18
{
19
    /**
20
     * endpoint of production.
21
     *
22
     * @const string
23
     */
24
    const ENDPOINT_PRODUCTION = 'https://api.moip.com.br';
25
26
    /**
27
     * endpoint of sandbox.
28
     *
29
     * @const string
30
     */
31
    const ENDPOINT_SANDBOX = 'https://sandbox.moip.com.br';
32
33
    /**
34
     * Client name.
35
     *
36
     * @const string
37
     **/
38
    const CLIENT = 'Moip SDK';
39
40
    /**
41
     * Authentication that will be added to the header of request.
42
     *
43
     * @var \Moip\MoipAuthentication
44
     */
45
    private $moipAuthentication;
46
47
    /**
48
     * Endpoint of request.
49
     *
50
     * @var \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX
51
     */
52
    private $endpoint;
53
54
    /**
55
     * @var Requests_Session HTTP session configured to use the moip API.
56
     */
57
    private $session;
58
59
    /**
60
     * Create a new aurhentication with the endpoint.
61
     *
62
     * @param \Moip\MoipAuthentication                                            $moipAuthentication
63
     * @param string|\Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX $endpoint
64
     */
65
    public function __construct(MoipAuthentication $moipAuthentication, $endpoint = self::ENDPOINT_PRODUCTION)
66
    {
67
        $this->moipAuthentication = $moipAuthentication;
68
        $this->endpoint = $endpoint;
0 ignored issues
show
Documentation Bug introduced by
It seems like $endpoint can also be of type string. However, the property $endpoint is declared as type object<Moip\Moip::ENDPOI...Moip::ENDPOINT_SANDBOX>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        $this->createNewSession();
70
    }
71
72
    /**
73
     * Creates a new Request_Session with all the default values.
74
     * A Session is created at construction.
75
     *
76
     * @param float $timeout         How long should we wait for a response?(seconds with a millisecond precision, default: 30, example: 0.01).
77
     * @param float $connect_timeout How long should we wait while trying to connect? (seconds with a millisecond precision, default: 10, example: 0.01)
78
     */
79
    public function createNewSession($timeout = 30.0, $connect_timeout = 30.0)
80
    {
81
        if (function_exists('posix_uname')) {
82
            $uname = posix_uname();
83
            $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s; %s)',
84
                self::CLIENT, PHP_SAPI, PHP_VERSION, $uname['sysname'], $uname['machine']);
85
        } else {
86
            $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s)',
87
                self::CLIENT, PHP_SAPI, PHP_VERSION, PHP_OS);
88
        }
89
        $sess = new Requests_Session($this->endpoint);
90
        $sess->options['auth'] = $this->moipAuthentication;
91
        $sess->options['timeout'] = $timeout;
92
        $sess->options['connect_timeout'] = $connect_timeout;
93
        $sess->options['useragent'] = $user_agent;
94
        $this->session = $sess;
95
    }
96
97
    /**
98
     * Returns the http session created.
99
     *
100
     * @return Requests_Session
101
     */
102
    public function getSession()
103
    {
104
        return $this->session;
105
    }
106
107
    /**
108
     * Replace the http session by a custom one.
109
     *
110
     * @param Requests_Session $session
111
     */
112
    public function setSession($session)
113
    {
114
        $this->session = $session;
115
    }
116
117
    /**
118
     * Create a new Customer instance.
119
     *
120
     * @return \Moip\Resource\Customer
121
     */
122
    public function customers()
123
    {
124
        return new Customer($this);
125
    }
126
127
    /**
128
     * Create a new Account instance.
129
     *
130
     * @return \Moip\Resource\Account
131
     */
132
    public function accounts()
133
    {
134
        return new Account($this);
135
    }
136
137
    /**
138
     * Create a new Entry instance.
139
     *
140
     * @return \Moip\Resource\Entry
141
     */
142
    public function entries()
143
    {
144
        return new Entry($this);
145
    }
146
147
    /**
148
     * Create a new Orders instance.
149
     *
150
     * @return \Moip\Resource\Orders
151
     */
152
    public function orders()
153
    {
154
        return new Orders($this);
155
    }
156
157
    /**
158
     * Create a new Payment instance.
159
     *
160
     * @return \Moip\Resource\Payment
161
     */
162
    public function payments()
163
    {
164
        return new Payment($this);
165
    }
166
167
    /**
168
     * Create a new Multiorders instance.
169
     *
170
     * @return \Moip\Resource\Multiorders
171
     */
172
    public function multiorders()
173
    {
174
        return new Multiorders($this);
175
    }
176
177
    /**
178
     * Create a new Transfers.
179
     *
180
     * @return \Moip\Resource\Transfers
181
     */
182
183
    /**
184
     * Create a new Transfers instance.
185
     *
186
     * @return Transfers
187
     */
188
    public function transfers()
189
    {
190
        return new Transfers($this);
191
    }
192
193
    /**
194
     * Get the endpoint.
195
     *
196
     * @return \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX
197
     */
198
    public function getEndpoint()
199
    {
200
        return $this->endpoint;
201
    }
202
}
203