Passed
Pull Request — master (#1)
by Vince
01:27
created

responsible::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\
11
 * @version 1.2
12
 *
13
 * @author Vince scarpa <[email protected]>
14
 *
15
 */
16
namespace responsible;
17
18
use responsible\core as responsibleCore;
19
use responsible\core\configuration;
20
use responsible\core\user;
21
use responsible\core\headers;
22
23
class responsible
24
{
25
    use \responsible\core\traits\optionsTrait;
26
27
    /**
28
     * [$config Variable store for the Responsible API config set]
29
     * @var array
30
     */
31
    private $config;
32
33
    /**
34
     * [$defaults Variable store for the Responsible API defaults set]
35
     * @var array
36
     */
37
    private $defaults;
38
39
    /**
40
     * [$server Core server object]
41
     * @var object
42
     */
43
    private $server;
44
45
    /**
46
     * [$response Response data]
47
     * @var array|object
48
     */
49
    private static $response;
50
51
    /**
52
     * [$requestType Header request response format]
53
     * @var string
54
     */
55
    private $requestType = 'json';
56
57
    /**
58
     * [__construc :: Construct the Responsible API]
59
     * @param array $DEFAULTS   
60
     *        environment settings
61
     * @param array  $options  
62
     *        API options
63
     */
64
    public function __construct(array $options = [])
65
    {
66
        /**
67
         * Initiate the Responsible API configuration and options
68
         */
69
        $this->setConfig($options);
70
71
        $this->setRequestType(($options['requestType']) ?? 'json');
72
73
        $this->setRateLimit(($options['rateLimit']) ?? 100);
74
75
        $this->setRateWindow(($options['rateWindow']) ?? 'MINUTE');
76
77
        /**
78
         * Initiate the Responsible API server
79
         */
80
        $this->server();
81
    }
82
83
    /**
84
     * [setConfig Set the ResponsibleAPI configuration]
85
     * @return void
86
     */
87
    private function setConfig($options)
88
    {
89
        $config = new configuration\config;
90
        $config->baseApiRoot(dirname(__DIR__));
91
        $config->responsibleDefault($options);
92
93
        $this->setOptions($config->getOptions());
94
        $this->config($config->getConfig());
95
        $this->defaults($config->getDefaults());
96
    }
97
98
    /**
99
     * [server :: Initiate the Responsible core server]
100
     * @return void
101
     */
102
    private function server()
103
    {
104
        $route = ($options['route']) ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to never exist and therefore isset should always be false.
Loading history...
105
106
        /**
107
         * [$this->server :: Set the a new API server object]
108
         * @var responsibleCore [Alias for responsible\core]
109
         */
110
        $this->server = new responsibleCore\server(
111
            $this->getConfig(),
112
            $this->getOptions(),
113
            true
114
        );
115
116
        $this->server
117
        // Set the header request format
118
            ->requestType($this->getRequestType())
119
        // Set the rate limit and timeframe for API connection limits
120
            ->rateLimit(
121
                $this->getRateLimit(),
122
                $this->getRateWindow()
123
            )
124
        // Authenticate the API connections
125
            ->authenticate()
126
        // Build the APIs internal router
127
            ->route($route)
128
        ;
129
130
        self::$response = $this->server->response();
131
    }
132
133
    /**
134
     * [getApiData Get the Responsible API router data]
135
     * @return array
136
     */
137
    public function Router()
138
    {
139
        return $this->server->getRouter();
140
    }
141
142
    /**
143
     * [response :: Get the final API response as an output]
144
     * @return object|array
145
     */
146
    public function responseData($debug = 'coredata')
147
    {
148
        return $this->server->response($debug);
149
    }
150
151
    /**
152
     * [config Set the Responsible API configuration]
153
     * @return void
154
     */
155
    private function config($config)
156
    {
157
        $this->config = $config;
158
    }
159
160
    /**
161
     * [getConfig Get the stored Responsible API configuration]
162
     * @return array
163
     */
164
    public function getConfig()
165
    {
166
        return $this->config;
167
    }
168
169
    /**
170
     * [defaults Set the Responsible API defaults]
171
     * Configuration and Options merged
172
     * @return void
173
     */
174
    private function defaults($defaults)
175
    {
176
        $this->defaults = $defaults;
177
    }
178
179
    /**
180
     * [getDefaults Get the stored Responsible API defaults]
181
     * @return array
182
     */
183
    public function getDefaults()
184
    {
185
        return $this->defaults;
186
    }
187
188
    /**
189
     * [setRequestType :: Header request response format]
190
     * @param string $type
191
     */
192
    private function setRequestType($type)
193
    {
194
        $this->requestType = $type;
195
    }
196
197
    /**
198
     * [getRequestType :: Get the header request format]
199
     * @return string
200
     */
201
    private function getRequestType()
202
    {
203
        return $this->requestType;
204
    }
205
206
    /**
207
     * [setRateLimit :: Set the Responsible API ratelimit]
208
     * How many API requests a connection is allowed
209
     * in a certain timeframe
210
     *
211
     * EG: 100 requests per minute
212
     *
213
     * @param integer $limit
214
     */
215
    private function setRateLimit($limit)
216
    {
217
        $this->requestLimit = $limit;
0 ignored issues
show
Bug Best Practice introduced by
The property requestLimit does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
218
    }
219
220
    /**
221
     * [getRateLimit :: Get the Responsible API ratelimit]
222
     * @return integer
223
     */
224
    private function getRateLimit()
225
    {
226
        return $this->requestLimit;
227
    }
228
229
    /**
230
     * [setRateWindow Set the Responsible API window for rate limits]
231
     * The window is a range set for API connection requests
232
     *
233
     * @see setRateLimit()
234
     * @param string|integer $frame
235
     */
236
    private function setRateWindow($frame)
237
    {
238
        if (is_numeric($frame)) {
239
            $this->requestRateWindow = $frame;
0 ignored issues
show
Bug Best Practice introduced by
The property requestRateWindow does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
240
            return;
241
        }
242
243
        $this->requestRateWindow = $frame;
244
    }
245
246
    /**
247
     * [getRateWindow Get the timeframe set for rate limits]
248
     * @return integer|string
249
     */
250
    private function getRateWindow()
251
    {
252
        return $this->requestRateWindow;
253
    }
254
255
    /**
256
     * **************************************
257
     * PUBLIC FACTORY METHODS
258
     * ***************************************
259
     */
260
261
    /**
262
     * [API Initiate the Responsible API]
263
     * @param array $options
264
     * @return self|object
265
     */
266
    public static function API(array $options = [])
267
    {
268
        return new self($options);
269
    }
270
271
    /**
272
     * [unauthorised Set a custom unauthorized header]
273
     * @return void
274
     */
275
    public static function unauthorised()
276
    {
277
        (new headers\header)->unauthorised();
278
    }
279
280
    /**
281
     * [response Get the Responsible API response]
282
     * @return array|object
283
     */
284
    public static function response($echo = false)
285
    {
286
        if ($echo) {
287
            print_r(self::$response);
288
            return;
289
        }
290
        return self::$response;
291
    }
292
293
    /**
294
     * [createUser Create a new user access]
295
     * @param  string $name
296
     * @param  string $mail
297
     * @return array
298
     */
299
    public static function createUser($name, $mail, array $options = [])
300
    {
301
        return (new user\user)
302
            ->setOptions($options)
303
            ->credentials($name, $mail)
304
            ->create()
305
        ;
306
    }
307
308
    /**
309
     * [updateUser Update a user account]
310
     * @param  array $properties
311
     * @param  array $options
312
     * @return array
313
     */
314
    public static function updateUser($properties, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

314
    public static function updateUser($properties, /** @scrutinizer ignore-unused */ array $options = [])

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

Loading history...
315
    {
316
        return (new user\user)
317
            ->update($properties)
318
        ;
319
    }
320
321
    /**
322
     * [loadUser Load a stored account]
323
     * @param  interger|string $property
0 ignored issues
show
Bug introduced by
The type responsible\interger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
324
     * @param  string $type
325
     * @return array
326
     */
327
    public static function loadUser($property, array $options = [])
328
    {
329
        $loadBy = (isset($options['loadBy']) && !empty($options['loadBy']))
330
        ? $options['loadBy'] : 'account_id';
331
332
        $getJWT = (isset($options['getJWT']) && is_bool($options['getJWT']))
333
        ? $options['getJWT'] : true;
334
335
        $getSecretAppend = (isset($options['secret']) && ($options['secret'] == 'append') )
336
        ? $options['secret'] : false;
337
338
        return (new user\user)
339
            ->setOptions($options)
340
            ->load(
341
                $property,
342
                array(
343
                    'loadBy' => $loadBy,
344
                    'getJWT' => $getJWT,
345
                    'secret' => $getSecretAppend
346
                )
347
        );
348
    }
349
}
350