GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Gjk::setConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9332
1
<?php
2
/**
3
 * This file is part of the mucts.com.
4
 *
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 *
8
 * @version 1.0
9
 * @author herry<[email protected]>
10
 * @copyright © 2020 MuCTS.com All Rights Reserved.
11
 */
12
13
namespace MuCTS\Laravel\GuiJK;
14
15
16
use Exception;
17
use Illuminate\Support\Arr;
18
use MuCTS\Laravel\GuiJK\Config\Config;
19
use MuCTS\Laravel\GuiJK\Exceptions\InvalidArgumentException;
20
use MuCTS\Laravel\GuiJK\Exceptions\Exception as GjkException;
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Exception\RequestException;
23
24
class Gjk
25
{
26
    use Sign;
27
28
    /** @var Config */
29
    private $config;
30
31
    public function __construct(?array $config)
32
    {
33
        $config = $config ?? config('gjk');
34
        $this->setConfig(new Config($config));
35
    }
36
37
    /**
38
     * Modify configuration information.
39
     *
40
     * @param Config $config
41
     * @return Gjk
42
     */
43
    public function setConfig(Config $config): self
44
    {
45
        $rules = [
46
            'url' => 'required|active_url',
47
            'app_id' => 'required|int',
48
            'secret_key' => 'required|string',
49
            'version' => 'required|int'
50
        ];
51
        $validator = validator($config->all(), [$rules]);
52
        if ($validator->fails()) {
53
            throw new InvalidArgumentException('Please set the correct configuration,error:' . $validator->errors());
54
        }
55
        $this->config = $config;
56
        return $this;
57
    }
58
59
    /**
60
     * Get GuiJk Server API request url
61
     *
62
     * @return string|null
63
     */
64
    protected function getUrl(): string
65
    {
66
        return $this->config->get('url');
67
    }
68
69
    /**
70
     * Get secret key
71
     *
72
     * @return string
73
     */
74
    protected function getSecretKey(): string
75
    {
76
        return $this->config->get('secret_key');
77
    }
78
79
    /**
80
     * get basic params
81
     *
82
     * @return array
83
     * @throws Exception
84
     */
85
    protected function getBasicParams(): array
86
    {
87
        return [
88
            "app_id" => $this->config->get('app_id'),
89
            "c_ver" => $this->config->get('version'),
90
            "nonce_str" => str_random(mt_rand(6, 32))
91
        ];
92
    }
93
94
    /**
95
     * Gjk Api Request
96
     *
97
     * @param string $route
98
     * @param array $param
99
     * @param int $timeOut
100
     * @return array|null
101
     * @throws Exception
102
     */
103
    public function request(string $route, array $param = [], $timeOut = 15): ?array
104
    {
105
        $param = ['user_id' => Arr::pull($param, 'user_id', mt_rand(1000000, 3000000))]
106
            + $this->getBasicParams() + $param;
107
        Arr::set($param, 'sign', $this->generateSign($param, $this->getSecretKey()));
108
        $client = new Client(["headers" => [], "timeout" => $timeOut]);
109
        $options = ['json' => $param];
110
        try {
111
            $response = $client->request('POST', rtrim($this->getUrl(), '/') . '/' . ltrim($route, '/'), $options);
112
        } catch (RequestException $exception) {
113
            $response = $exception->getResponse();
114
        } catch (Exception $exception) {
115
            throw new GjkException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
116
        }
117
        $response = $response ? $response->getBody() : null;
118
        return $response ? json_decode($response, true) : null;
119
    }
120
}