Passed
Push — master ( 4fff34...615c29 )
by Jacques
01:51
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.0856
cc 1
eloc 14
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * SmartCall Restful API (v3) HTTP Client
4
 *
5
 * @author    Jacques Marneweck <[email protected]>
6
 * @copyright 2017 Jacques Marneweck.  All rights strictly reserved.
7
 * @license   MIT
8
 */
9
10
namespace Jacques\Smartcall\HttpClient;
11
12
class Client extends \GuzzleHttp\Client
13
{
14
    /**
15
     * @const string Version number
16
     */
17
    const VERSION = '0.0.1';
18
19
    /**
20
     * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost
21
     * (127.0.0.1).
22
     *
23
     * @var array[]
24
     */
25
    protected $options = [
26
        'scheme'   => 'https',
27
        'hostname' => 'localhost',
28
        'port'     => '8080',
29
    ];
30
31
    /**
32
     * @param   $options array
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36 1
    public function __construct($options = [])
37
    {
38
        /*
39
         * Allow on instantiation to overwrite the defaults
40
         */
41 1
        $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...
42 1
            $this->options,
43 1
            $options
44
        );
45
        $config = [
46 1
            'base_uri' => sprintf(
47 1
                '%s://%s:%s/',
48 1
                $this->options['scheme'],
49 1
                $this->options['hostname'],
50 1
                $this->options['port']
51
            ),
52
            'verify' => false,
53
            'headers' => [
54 1
                'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(),
55
            ],
56
        ];
57 1
        parent::__construct($config);
58 1
    }
59
60
    /**
61
     * Authenticate and get Bearer token from SmartCall
62
     *
63
     * @param string $username
64
     * @param string $password
65
     *
66
     * @throws Exception
67
     *
68
     * @return array
69
     */
70
    public function auth($username, $password)
71
    {
72
        try {
73
            $response = $this->post(
74
                '/webservice/auth',
75
                [
76
                    'headers' => [
77
                        'Authorization' => sprintf(
78
                            'Basic %s',
79
                            base64_encode(
80
                                sprintf(
81
                                    '%s:%s',
82
                                    $username,
83
                                    $password
84
                                )
85
                            )
86
                        ),
87
                    ],
88
                ]
89
            );
90
91
            return [
92
                'status'    => 'ok',
93
                'http_code' => $response->getStatusCode(),
94
                'body'      => (string) $response->getBody(),
95
            ];
96
        } catch (\GuzzleHttp\Exception\ServerException $e) {
97
            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...
98
        }
99
    }
100
101
    /**
102
     * Test SmartCall is responding.
103
     *
104
     * @throws Exception
105
     *
106
     * @return array
107
     */
108 1
    public function ping()
109
    {
110
        try {
111 1
            $response = $this->get(
112 1
                '/webservice/test/ping'
113
            );
114
115
            return [
116 1
                'status'    => 'ok',
117 1
                'http_code' => $response->getStatusCode(),
118 1
                'body'      => (string) $response->getBody(),
119
            ];
120
        } catch (\GuzzleHttp\Exception\ServerException $e) {
121
            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...
122
        }
123
    }
124
}
125