Passed
Push — master ( 82a9a3...7c9524 )
by Jacques
01:49
created

Client::authFlush()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 30
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 30
loc 30
ccs 0
cts 15
cp 0
rs 8.8571
cc 2
eloc 18
nc 3
nop 2
crap 6
1
<?php
2
/**
3
 * SmartCall Restful API (v3) HTTP Client
4
 *
5
 * PLEASE NOTE: The interface is very fluid while the intial integration
6
 * is taking place.  It will be refactored in the near future.
7
 *
8
 * @author    Jacques Marneweck <[email protected]>
9
 * @copyright 2017-2018 Jacques Marneweck.  All rights strictly reserved.
10
 * @license   MIT
11
 */
12
13
namespace Jacques\Smartcall\HttpClient;
14
15
class Client extends \GuzzleHttp\Client
16
{
17
    /**
18
     * @const string Version number
19
     */
20
    const VERSION = '0.0.1';
21
22
    /**
23
     * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost
24
     * (127.0.0.1).
25
     *
26
     * @var array[]
27
     */
28
    protected $options = [
29
        'scheme'   => 'https',
30
        'hostname' => 'localhost',
31
        'port'     => '8080',
32
        'token'    => null,
33
        'username' => null,
34
        'password' => null,
35
    ];
36
37
    /**
38
     * @param   $options array
39
     */
40 5
    public function __construct($options = [])
41
    {
42
        /*
43
         * Allow on instantiation to overwrite the defaults
44
         */
45 5
        $this->options = array_merge(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->options, $options) of type array is incompatible with the declared type array<integer,array> of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

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

Loading history...
46 5
            $this->options,
47 5
            $options
48
        );
49
        $config = [
50 5
            'base_uri' => sprintf(
51 5
                '%s://%s:%s/',
52 5
                $this->options['scheme'],
53 5
                $this->options['hostname'],
54 5
                $this->options['port']
55
            ),
56
            'verify' => false,
57
            'headers' => [
58 5
                'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(),
59
            ],
60
        ];
61 5
        parent::__construct($config);
62 5
    }
63
64
    /**
65
     * Set the bearer token
66
     *
67
     * @param string $token Bearer Token from Auth request
68
     */
69 3
    public function setBearerToken($token)
70
    {
71 3
        $this->options['token'] = $token;
72 3
    }
73
74
    /**
75
     * Authenticate and get Bearer token from SmartCall
76
     *
77
     * @param string $username
78
     * @param string $password
79
     *
80
     * @throws Exception
81
     *
82
     * @return array
83
     */
84 View Code Duplication
    public function auth($username, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        try {
87
            $response = $this->post(
88
                '/webservice/auth',
89
                [
90
                    'headers' => [
91
                        'Authorization' => sprintf(
92
                            'Basic %s',
93
                            base64_encode(
94
                                sprintf(
95
                                    '%s:%s',
96
                                    $username,
97
                                    $password
98
                                )
99
                            )
100
                        ),
101
                    ],
102
                ]
103
            );
104
105
            return [
106
                'status'    => 'ok',
107
                'http_code' => $response->getStatusCode(),
108
                'body'      => (string) $response->getBody(),
109
            ];
110
        } catch (\GuzzleHttp\Exception\ServerException $e) {
111
            return $this->parseError($e);
0 ignored issues
show
Documentation Bug introduced by
The method parseError does not exist on object<Jacques\Smartcall\HttpClient\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
112
        }
113
    }
114
115
    /**
116
     * Authenticate and invalidates all the user allocated tokens
117
     *
118
     * @param string $username
0 ignored issues
show
Bug introduced by
There is no parameter named $username. 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...
119
     * @param string $password
0 ignored issues
show
Bug introduced by
There is no parameter named $password. 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...
120
     *
121
     * @throws Exception
122
     *
123
     * @return array
124
     */
125 2
    public function authDelete()
126
    {
127
        try {
128 2
            $response = $this->delete(
129 2
                '/webservice/auth',
130
                [
131
                    'headers' => [
132 2
                        'Authorization' => sprintf(
133 2
                            'Bearer %s',
134 2
                            $this->options['token']
135
                        ),
136
                    ],
137
                ]
138
            );
139
140
            return [
141 1
                'status'    => 'ok',
142 1
                'http_code' => $response->getStatusCode(),
143 1
                'body'      => (string) $response->getBody(),
144
            ];
145 1
        } catch (\GuzzleHttp\Exception\ServerException $e) {
146
            return $this->parseError($e);
0 ignored issues
show
Documentation Bug introduced by
The method parseError does not exist on object<Jacques\Smartcall\HttpClient\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
        }
148
    }
149
150
    /**
151
     * Authenticate and invalidates all the user allocated tokens
152
     *
153
     * @param string $username
154
     * @param string $password
155
     *
156
     * @throws Exception
157
     *
158
     * @return array
159
     */
160 View Code Duplication
    public function authFlush($username, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        try {
163
            $response = $this->delete(
164
                '/webservice/auth/token',
165
                [
166
                    'headers' => [
167
                        'Authorization' => sprintf(
168
                            'Basic %s',
169
                            base64_encode(
170
                                sprintf(
171
                                    '%s:%s',
172
                                    $username,
173
                                    $password
174
                                )
175
                            )
176
                        ),
177
                    ],
178
                ]
179
            );
180
181
            return [
182
                'status'    => 'ok',
183
                'http_code' => $response->getStatusCode(),
184
                'body'      => (string) $response->getBody(),
185
            ];
186
        } catch (\GuzzleHttp\Exception\ServerException $e) {
187
            return $this->parseError($e);
0 ignored issues
show
Documentation Bug introduced by
The method parseError does not exist on object<Jacques\Smartcall\HttpClient\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
188
        }
189
    }
190
191
    /**
192
     * Authenticate and gets the number of available session tokens
193
     *
194
     * @param string $username
195
     * @param string $password
196
     *
197
     * @throws Exception
198
     *
199
     * @return array
200
     */
201 View Code Duplication
    public function authToken($username, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        try {
204
            $response = $this->get(
205
                '/webservice/auth/token',
206
                [
207
                    'headers' => [
208
                        'Authorization' => sprintf(
209
                            'Basic %s',
210
                            base64_encode(
211
                                sprintf(
212
                                    '%s:%s',
213
                                    $username,
214
                                    $password
215
                                )
216
                            )
217
                        ),
218
                    ],
219
                ]
220
            );
221
222
            return [
223
                'status'    => 'ok',
224
                'http_code' => $response->getStatusCode(),
225
                'body'      => (string) $response->getBody(),
226
            ];
227
        } catch (\GuzzleHttp\Exception\ServerException $e) {
228
            return $this->parseError($e);
0 ignored issues
show
Documentation Bug introduced by
The method parseError does not exist on object<Jacques\Smartcall\HttpClient\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
229
        }
230
    }
231
232
    /**
233
     * Test SmartCall is responding.
234
     *
235
     * @throws Exception
236
     *
237
     * @return array
238
     */
239 1
    public function ping()
240
    {
241
        try {
242 1
            $response = $this->get(
243 1
                '/webservice/test/ping'
244
            );
245
246
            return [
247 1
                'status'    => 'ok',
248 1
                'http_code' => $response->getStatusCode(),
249 1
                'body'      => (string) $response->getBody(),
250
            ];
251
        } catch (\GuzzleHttp\Exception\ServerException $e) {
252
            return $this->parseError($e);
0 ignored issues
show
Documentation Bug introduced by
The method parseError does not exist on object<Jacques\Smartcall\HttpClient\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
253
        }
254
    }
255
}
256