Completed
Push — master ( 2e7298...3749b5 )
by
unknown
11s
created

Moip::getEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
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\Entry;
7
use Moip\Resource\Multiorders;
8
use Moip\Resource\Orders;
9
use Moip\Resource\Payment;
10
use Requests_Session;
11
12
class Moip
13
{
14
    /**
15
     * endpoint of production.
16
     *
17
     * @const string
18
     */
19
    const ENDPOINT_PRODUCTION = 'https://api.moip.com.br';
20
    /**
21
     * endpoint of sandbox.
22
     *
23
     * @const string
24
     */
25
    const ENDPOINT_SANDBOX = 'https://sandbox.moip.com.br';
26
27
    /**
28
     * Client name.
29
     *
30
     * @const string
31
     **/
32
    const CLIENT = 'Moip SDK';
33
34
    /**
35
     * Authentication that will be added to the header of request.
36
     *
37
     * @var \Moip\MoipAuthentication
38
     */
39
    private $moipAuthentication;
40
41
    /**
42
     * Endpoint of request.
43
     *
44
     * @var \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX
45
     */
46
    private $endpoint;
47
48
    /**
49
     * @var Requests_Session HTTP session configured to use the moip API.
50
     */
51
    private $session;
52
53
    /**
54
     * Create a new aurhentication with the endpoint.
55
     *
56
     * @param \Moip\MoipAuthentication               $moipAuthentication
57
     * @param \Moip\Moip::ENDPOINT_PRODUCTION|string $endpoint
58
     */
59
    public function __construct(MoipAuthentication $moipAuthentication, $endpoint = self::ENDPOINT_PRODUCTION)
60
    {
61
        $this->moipAuthentication = $moipAuthentication;
62
        $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...
63
        $this->createNewSession();
64
    }
65
66
    /**
67
     * Creates a new Request_Session with all the default values.
68
     * A Session is created at construction.
69
     *
70
     * @param float $timeout         How long should we wait for a response?(seconds with a millisecond precision, default: 30, example: 0.01).
71
     * @param float $connect_timeout How long should we wait while trying to connect? (seconds with a millisecond precision, default: 10, example: 0.01)
72
     */
73
    public function createNewSession($timeout = 30.0, $connect_timeout = 30.0)
74
    {
75
        if (function_exists('posix_uname')) {
76
            $uname = posix_uname();
77
            $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s; %s)',
78
                self::CLIENT, PHP_SAPI, PHP_VERSION, $uname['sysname'], $uname['machine']);
79
        } else {
80
            $user_agent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s %s; %s)',
81
                self::CLIENT, PHP_SAPI, PHP_VERSION, PHP_OS);
82
        }
83
        $sess = new Requests_Session($this->endpoint);
84
        $sess->options['auth'] = $this->moipAuthentication;
85
        $sess->options['timeout'] = $timeout;
86
        $sess->options['connect_timeout'] = $connect_timeout;
87
        $sess->options['useragent'] = $user_agent;
88
        $this->session = $sess;
89
    }
90
91
    /**
92
     * Returns the http session created.
93
     *
94
     * @return Requests_Session
95
     */
96
    public function getSession()
97
    {
98
        return $this->session;
99
    }
100
101
    /**
102
     * Replace the http session by a custom one.
103
     *
104
     * @param Requests_Session $session
105
     */
106
    public function setSession($session)
107
    {
108
        $this->session = $session;
109
    }
110
111
    /**
112
     * Create a new Customer instance.
113
     *
114
     * @return \Moip\Resource\Customer
115
     */
116
    public function customers()
117
    {
118
        return new Customer($this);
119
    }
120
121
    /**
122
     * Create a new Entry instance.
123
     *
124
     * @return \Moip\Resource\Entry
125
     */
126
    public function entries()
127
    {
128
        return new Entry($this);
129
    }
130
131
    /**
132
     * Create a new Orders instance.
133
     *
134
     * @return \Moip\Resource\Orders
135
     */
136
    public function orders()
137
    {
138
        return new Orders($this);
139
    }
140
141
    /**
142
     * Create a new Payment instance.
143
     *
144
     * @return \Moip\Resource\Payment
145
     */
146
    public function payments()
147
    {
148
        return new Payment($this);
149
    }
150
151
    /**
152
     * Create a new Multiorders instance.
153
     *
154
     * @return \Moip\Resource\Multiorders
155
     */
156
    public function multiorders()
157
    {
158
        return new Multiorders($this);
159
    }
160
161
    /**
162
     * Get the endpoint.
163
     *
164
     * @return \Moip\Moip::ENDPOINT_PRODUCTION|\Moip\Moip::ENDPOINT_SANDBOX
165
     */
166
    public function getEndpoint()
167
    {
168
        return $this->endpoint;
169
    }
170
}
171