Completed
Push — master ( c3bf1d...4da758 )
by Andrii
02:28
created

AbstractConnection::enableAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
use Closure;
14
use hiqdev\hiart\stream\Request;
15
use Yii;
16
use yii\base\Component;
17
use yii\base\InvalidParamException;
18
use yii\helpers\Json;
19
20
/**
21
 * Abstract connection class.
22
 */
23
abstract class AbstractConnection extends Component implements ConnectionInterface
24
{
25
    const EVENT_AFTER_OPEN = 'afterOpen';
26
27
    /**
28
     * @var string to be specified in concrete implementation
29
     */
30
    public $queryBuilderClass;
31
32
    public $requestClass = Request::class;
33
34
    public $commandClass = Command::class;
35
36
    public $queryClass = Query::class;
37
38
    public $activeQueryClass = ActiveQuery::class;
39
40
    public static $dbname = 'hiart';
41
42
    public $name = 'hiart';
43
44
    public $userAgent = 'HiArt/0.x';
45
46
    public $baseUri;
47
48
    /**
49
     * @var array transport config will be used in Request for handler or proxy request
50
     */
51
    public $config = [];
52
53
    /**
54
     * @var object request handler common for all requests of this connection
55
     */
56
    protected $_handler;
57
58
    /**
59
     * @var AbstractQueryBuilder the query builder for this connection
60
     */
61
    protected $_builder;
62
63
    /**
64
     * @var array authorization config
65
     */
66
    protected $_auth = [];
67
68
    /**
69
     * @var bool is auth disabled
70
     */
71
    protected $_disabledAuth = false;
72
73
    /**
74
     * @var Closure callback to test if API response has error
75
     * The function signature: `function ($response)`
76
     * Must return `null`, if the response does not contain an error
77
     */
78
    protected $_errorChecker;
79
80
    /**
81
     * @param null $name
82
     * @return ConnectionInterface|AbstractConnection
83
     */
84 1
    public static function getDb($name = null, $class = ConnectionInterface::class)
85
    {
86 1
        if ($name) {
87
            return Yii::$app->get($name);
88
        }
89 1
        if (Yii::$container->hasSingleton($class)) {
90
            return Yii::$container->get($class);
91
        }
92
93 1
        return Yii::$app->get(static::$dbname);
94
    }
95
96 1
    public function setAuth($auth)
97
    {
98 1
        $this->_auth = $auth;
99 1
    }
100
101
    /**
102
     * Returns auth settings.
103
     * @return array
104
     */
105 2
    public function getAuth()
106
    {
107 2
        if ($this->_disabledAuth) {
108
            return [];
109
        }
110 2
        if ($this->_auth instanceof Closure) {
111
            $this->_auth = call_user_func($this->_auth, $this);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func($this->_auth, $this) of type * is incompatible with the declared type array of property $_auth.

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...
112
        }
113
114 2
        return $this->_auth;
115
    }
116
117
    public function disableAuth()
118
    {
119
        $this->_disabledAuth = true;
120
    }
121
122
    public function enableAuth()
123
    {
124
        $this->_disabledAuth = false;
125
    }
126
127
    /**
128
     * Closes the connection when this component is being serialized.
129
     * @return array
130
     */
131
    public function __sleep()
132
    {
133
        return array_keys(get_object_vars($this));
134
    }
135
136
    /**
137
     * Returns the name of the DB driver for the current [[dsn]].
138
     * @return string name of the DB driver
139
     */
140
    public function getDriverName()
141
    {
142
        return 'hiart';
143
    }
144
145
    /**
146
     * Creates a command for execution.
147
     * @param array $config the configuration for the Command class
148
     * @return Command the DB command
149
     */
150 2
    public function createCommand(array $config = [])
151
    {
152 2
        $config['db'] = $this;
153
154 2
        return new $this->commandClass($config);
155
    }
156
157
    /**
158
     * @return AbstractQueryBuilder the query builder for this connection
159
     */
160 3
    public function getQueryBuilder()
161
    {
162 3
        if ($this->_builder === null) {
163 2
            $this->_builder = new $this->queryBuilderClass($this);
164 2
        }
165
166 3
        return $this->_builder;
167
    }
168
169
    /**
170
     * Handler is created and set by request.
171
     * @see setHandler
172
     * @return object
173
     */
174
    public function getHandler()
175
    {
176
        return $this->_handler;
177
    }
178
179
    /**
180
     * Requests use this function to keep request handler.
181
     * @param object $handler
182
     */
183
    public function setHandler($handler)
184
    {
185
        $this->_handler = $handler;
186
    }
187
188
    /**
189
     * @return boolean
190
     */
191
    public function isDisabledAuth()
192
    {
193
        return $this->_disabledAuth;
194
    }
195
196
    /**
197
     * @param boolean $disabledAuth
198
     */
199
    public function setDisabledAuth($disabledAuth)
200
    {
201
        $this->_disabledAuth = $disabledAuth;
202
    }
203
204
    /**
205
     * Try to decode error information if it is valid json, return it if not.
206
     * @param $body
207
     * @return mixed
208
     */
209
    protected function decodeErrorBody($body)
210
    {
211
        try {
212
            $decoded = Json::decode($body);
213
            if (isset($decoded['error'])) {
214
                $decoded['error'] = preg_replace('/\b\w+?Exception\[/',
215
                    "<span style=\"color: red;\">\\0</span>\n               ", $decoded['error']);
216
            }
217
218
            return $decoded;
219
        } catch (InvalidParamException $e) {
220
            return $body;
221
        }
222
    }
223
224
    /**
225
     * Setter for errorChecker.
226
     * @param Closure $checker
227
     */
228
    public function setErrorChecker($checker)
229
    {
230
        $this->_errorChecker = $checker;
231
    }
232
233
    /**
234
     * Checks response method and raises exception if error found.
235
     * @param ResponseInterface $response response data from API
236
     * @throws ResponseErrorException when response is invalid
237
     */
238 2
    public function checkResponse(ResponseInterface $response)
239
    {
240 2
        if (isset($this->_errorChecker)) {
241
            $error = call_user_func($this->_errorChecker, $response);
242
        } else {
243 2
            $error = $this->getResponseError($response);
244
        }
245
246 2
        if ($error) {
247
            throw new ResponseErrorException($error, $response);
248
        }
249 2
    }
250
251
    /**
252
     * Method checks whether the response is an error.
253
     *
254
     * @param ResponseInterface $response
255
     * @return false|string the error text or boolean `false`, when the response is not an error
256
     */
257
    abstract public function getResponseError(ResponseInterface $response);
258
259
    protected $baseUriChecked;
260
261
    /**
262
     * Return API base uri.
263
     * Adds trailing slash if uri is domain only.
264
     * @return string
265
     */
266 4
    public function getBaseUri()
267
    {
268 4
        if (empty($this->baseUriChecked)) {
269 3
            if (preg_match('#^https?://[^/]+$#', $this->baseUri)) {
270 1
                $this->baseUri .= '/';
271 1
            }
272 3
            $this->baseUriChecked = true;
273 3
        }
274
275 4
        return $this->baseUri;
276
    }
277
278 2
    public function getUserAgent()
279
    {
280 2
        return $this->userAgent;
281
    }
282
}
283