Client::makeUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JulianoBailao\DomusApi;
4
5
use JulianoBailao\DomusApi\Core\Request;
6
use JulianoBailao\DomusApi\Exception\InvalidApiEndPointException;
7
use JulianoBailao\DomusApi\Exception\InvalidHostException;
8
9
class Client
10
{
11
    /**
12
     * The api host.
13
     *
14
     * @var string
15
     */
16
    protected $host;
17
18
    /**
19
     * The api port.
20
     *
21
     * @var string
22
     */
23
    protected $port;
24
25
    /**
26
     * Api usernamename / email.
27
     *
28
     * @var string
29
     */
30
    protected $username;
31
32
    /**
33
     * Api username password.
34
     *
35
     * @var string
36
     */
37
    protected $password;
38
39
    /**
40
     * Api authentication token.
41
     *
42
     * @var string
43
     */
44
    protected $token;
45
46
    /**
47
     * Domus branch id to login.
48
     *
49
     * @var int
50
     */
51
    protected $branchId;
52
53
    /**
54
     * The api client handler.
55
     *
56
     * @var mixed
57
     */
58
    protected $handler;
59
60
    /**
61
     * Create a new api client instance.
62
     *
63
     * @param string $host
64
     * @param string $port
65
     * @param string $username
66
     * @param string $password
67
     *
68
     * @throws InvalidHostException
69
     */
70 138
    public function __construct($host, $port, $username, $password)
71
    {
72 138
        $this->host = $host;
73 138
        $this->port = $port;
74 138
        $this->username = $username;
75 138
        $this->password = $password;
76 138
    }
77
78
    /**
79
     * Get the api uthetication token.
80
     *
81
     * @return string
82
     */
83 132
    public function getToken()
84
    {
85 132
        if ($this->token == null) {
86 132
            $this->auth();
87 132
        }
88
89 132
        return $this->token;
90
    }
91
92
    /**
93
     * Api authentication.
94
     *
95
     * @return self
96
     */
97 132
    protected function auth()
98
    {
99 132
        $request = new Request($this->handler);
100 132
        $this->token = $request->run('POST', $this->makeUrl('pedidovenda-rest/auth'), ['json' => [
101 132
            'login'    => $this->username,
102 132
            'password' => $this->password,
103 132
        ]])->token;
104
105 132
        return $this;
106
    }
107
108
    /**
109
     * Set the branch of endpoint operation.
110
     *
111
     * @param int $branchId
112
     *
113
     * @return self
114
     */
115 129
    public function setBranch($branchId)
116
    {
117 129
        $request = new Request($this->handler);
118 129
        $this->branchId = $request->run('PUT', $this->makeUrl('pedidovenda-rest/auth'), [
119
            'json' => [
120 129
                'id' => $branchId,
121 129
            ],
122
            'headers' => [
123 129
                'X-Session-ID' => $this->getToken(),
124 129
            ],
125 129
        ])->id;
126
127 129
        return $this;
128
    }
129
130
    /**
131
     * Make the endpoint full url.
132
     *
133
     * @param string $endpoint
134
     *
135
     * @return string
136
     */
137 135
    public function makeUrl($endpoint)
138
    {
139 135
        $host = rtrim(str_replace(['http://', 'https://'], null, $this->host), '/');
140 135
        $endpoint = ltrim($endpoint, '/');
141
142 135
        return $host.':'.$this->port.'/'.$endpoint;
143
    }
144
145
    /**
146
     * Sets the client handler.
147
     *
148
     * @param mixed $handler
149
     */
150 132
    public function setHandler($handler)
151
    {
152 132
        $this->handler = $handler;
153 132
    }
154
155
    /**
156
     * Gets the client handler.
157
     *
158
     * @param mixed $handler
0 ignored issues
show
Bug introduced by
There is no parameter named $handler. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
159
     */
160 126
    public function getHandler()
161
    {
162 126
        return $this->handler;
163
    }
164
165
    /**
166
     * Get the branch id.
167
     *
168
     * @return int
169
     */
170 3
    public function getBranchId()
171
    {
172 3
        return $this->branchId;
173
    }
174
175
    /**
176
     * Call a dinamyc api endpoint.
177
     *
178
     * @param string $method
179
     * @param array  $args
180
     *
181
     * @throws InvalidApiEndPointException
182
     *
183
     * @return mixed
184
     */
185 126
    public function __call($method, array $args)
186
    {
187 126
        $class = 'JulianoBailao\DomusApi\Endpoints\\'.ucfirst(strtolower($method));
188 126
        array_unshift($args, $this);
189
190 126
        return call_user_func_array([new \ReflectionClass($class), 'newInstance'], $args);
191
    }
192
}
193