Passed
Push — master ( e59495...facc33 )
by Isaac
02:07
created

SMSClient::configure()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0061

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 5
nop 1
dl 0
loc 29
ccs 17
cts 18
cp 0.9444
crap 6.0061
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Mediumart\Orange\SMS\Http;
4
5
use Mediumart\Orange\SMS\Http\Requests\AuthorizationRequest;
6
7
class SMSClient
8
{
9
    /**
10
     * Access token.
11
     *
12
     * @var string
13
     */
14
    protected $token;
15
16
    /**
17
     * Expires time.
18
     *
19
     * @var string
20
     */
21
    protected $expiresIn;
22
23
    /**
24
     * SMSCLient singleton instance.
25
     *
26
     * @var static
27
     */
28
    private static $instance;
29
30
    /**
31
     * SMSClient constructor.
32
     *
33
     * @throws \Error
34
     */
35 1
    private function __construct()
36
    {
37 1
    }
38
39
    /**
40
     * Set the access token.
41
     *
42
     * @param $token
43
     * @return $this
44
     */
45 12
    public function setToken($token)
46
    {
47 12
        $this->token = $token;
48
49 12
        return $this;
50
    }
51
52
    /**
53
     * Get the access token.
54
     *
55
     * @return mixed
56
     */
57 10
    public function getToken()
58
    {
59 10
        return $this->token;
60
    }
61
62
    /**
63
     * Set the expires_in in seconds
64
     *
65
     * @param $expiresIn
66
     * @return $this
67
     */
68 12
    public function setTokenExpiresIn($expiresIn)
69
    {
70 12
        $this->expiresIn = $expiresIn;
71
72 12
        return $this;
73
    }
74
75
    /**
76
     * Get the expire_in in seconds
77
     *
78
     * @return string
79
     */
80 3
    public function getTokenExpiresIn()
81
    {
82 3
        return $this->expiresIn;
83
    }
84
85
    /**
86
     * Configure the instance.
87
     *
88
     * @param array $options
89
     * @return $this
90
     */
91 13
    public function configure(...$options)
92
    {
93 13
        if (count($options) <= 0) {
94 2
            return $this;
95
        }
96
97 13
        switch (count($options)) {
98 13
            case 1:
99 11
                if (is_string($options[0])) {
100
                    $this
101 10
                        ->setTokenExpiresIn(null)
102 10
                        ->setToken($options[0]);
103 2
                } elseif (is_array($options[0])) {
104 2
                    $this->configureArrayOptions($options[0]);
105
                }
106 11
                break;
107
108 2
            case 2:
109 1
                $this->configureArrayOptions(
110 1
                    static::authorize($options[0], $options[1])
111
                );
112 1
                break;
113
114
            default:
115 1
                throw new \InvalidArgumentException('invalid argument count');
116
                break;
117
        }
118
119 12
        return $this;
120
    }
121
122
    /**
123
     * Configure instance using array options.
124
     *
125
     * @param  array  $options
126
     * @return $this
127
     */
128 3
    protected function configureArrayOptions(array $options)
129
    {
130 3
        if (array_key_exists('access_token', $options)) {
131 3
            $this->setToken($options['access_token']);
132
        }
133
134 3
        if (array_key_exists('expires_in', $options)) {
135 3
            $this->setTokenExpiresIn($options['expires_in']);
136
        }
137
138 3
        return $this;
139
    }
140
141
    /**
142
     * Execute a request against the Api server
143
     *
144
     * @param SMSClientRequest $request
145
     * @param bool $decodeJson
146
     * @return array
147
     */
148 7
    public function executeRequest(SMSClientRequest $request, $decodeJson = true)
149
    {
150 7
        $options = $request->options();
151
152 7
        if (! isset($options['headers']["Authorization"])) {
153 7
            $options['headers']["Authorization"] = "Bearer ". $this->getToken();
154
        }
155
156 7
        $response = $request->execute($options)->getBody();
157
158 7
        return $decodeJson ? json_decode($response, true) : $response;
159
    }
160
161
    /**
162
     * Get the client access token
163
     *
164
     * @param $clientID
165
     * @param $clientSecret
166
     * @return array
167
     */
168 2
    public static function authorize($clientID, $clientSecret)
169
    {
170 2
        return json_decode(
171 2
            (new AuthorizationRequest($clientID, $clientSecret))->execute()->getBody(), true
172
        );
173
    }
174
175
    /**
176
     * Get the prepared singleton instance of the client.
177
     *
178
     * @return SMSClient
179
     */
180 13
    public static function getInstance()
181
    {
182 13
        if (! static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
183 1
            static::$instance = new static();
184
        }
185
186 13
        return static::$instance->configure(...func_get_args());
187
    }
188
}
189