Completed
Push — master ( 6387a2...3f53e4 )
by Sercan
02:30
created

Gateway::getHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace CryptoMarkets\Common;
4
5
use CryptoMarkets\Common\Client;
6
7
abstract class Gateway
8
{
9
    /**
10
     * The HTTP client instance.
11
     *
12
     * @var \CryptoMarkets\Common\Client
13
     */
14
    protected $httpClient;
15
16
    /**
17
     * The gateway's options.
18
     *
19
     * @var array
20
     */
21
    protected $options = [];
22
23
    /**
24
     * Create a new Gateway instance.
25
     *
26
     * @param  array  $options
27
     * @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...
28
     */
29
    public function __construct(array $options = [])
30
    {
31
        $this->setOptions($options);
32
    }
33
34
    /**
35
     * Merge the custom options with the defaults.
36
     *
37
     * @param  array  $options
38
     * @return void
39
     */
40
    public function setOptions(array $options = [])
41
    {
42
        $this->options = array_replace($this->getDefaultOptions(), $options);
43
    }
44
45
    /**
46
     * Get the options.
47
     *
48
     * @return array
49
     */
50
    public function getOptions()
51
    {
52
        return $this->options;
53
    }
54
55
    /**
56
     * et the specified option value (if any).
57
     *
58
     * @param  string  $key
59
     * @param  mixed  $default
60
     * @return mixed
61
     */
62
    public function getOption($key, $default = null)
0 ignored issues
show
Unused Code introduced by
The parameter $default is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        return $this->options[$key];
65
    }
66
67
    /**
68
     * Get the default options.
69
     *
70
     * @return array
71
     */
72
    public function getDefaultOptions()
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * Get the gateway name.
79
     *
80
     * @return string
81
     */
82
    abstract public function getName();
83
84
    /**
85
     * Create a new request object.
86
     *
87
     * @param  string  $class
88
     * @param  array  $params
89
     * @return \CryptoMarkets\Common\Http\Request
90
     */
91
    public function createRequest($class, array $params = [])
92
    {
93
        $instance = new $class($this->getHttpClient());
94
95
        return $instance->configure(array_replace($this->getOptions(), $params));
96
    }
97
98
    /**
99
     * Get a instance of the HTTP client.
100
     *
101
     * @return \CryptoMarkets\Common\Client
102
     */
103
    public function getHttpClient()
104
    {
105
        if (is_null($this->httpClient)) {
106
            $this->setHttpClient(new Client);
107
        }
108
109
        return $this->httpClient;
110
    }
111
112
    /**
113
     * Set the HTTP client instance.
114
     *
115
     * @param  \CryptoMarkets\Common\Client  $client
116
     * @return $this
117
     */
118
    public function setHttpClient(Client $client)
119
    {
120
        $this->httpClient = $client;
121
122
        return $this;
123
    }
124
}
125