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.
Passed
Pull Request — master (#133)
by Yong
03:21
created

LocationService::updateEndpoints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Regions;
4
5
use Exception;
6
use RuntimeException;
7
use AlibabaCloud\Client\SDK;
8
use AlibabaCloud\Client\Config\Config;
9
use AlibabaCloud\Client\Request\Request;
10
use AlibabaCloud\Client\Filter\ApiFilter;
11
use AlibabaCloud\Client\Filter\HttpFilter;
12
use AlibabaCloud\Client\Filter\ClientFilter;
13
use AlibabaCloud\Client\Exception\ClientException;
14
use AlibabaCloud\Client\Exception\ServerException;
15
16
/**
17
 * Class LocationService
18
 *
19
 * @package   AlibabaCloud\Client\Regions
20
 */
21
class LocationService
22
{
23
    /**
24
     * Global Region Name
25
     */
26
    const GLOBAL_REGION = 'global';
27
28
    /**
29
     * @var array
30
     */
31
    protected static $hosts = [];
32
33
    /**
34
     * @var Request
35
     */
36
    protected $request;
37
38
    /**
39
     * LocationService constructor.
40
     *
41
     * @param Request $request
42
     */
43 14
    private function __construct(Request $request)
44
    {
45 14
        $this->request = $request;
46 14
    }
47
48
    /**
49
     * @deprecated
50
     * @codeCoverageIgnore
51
     */
52
    public static function findProductDomain()
53
    {
54
        throw new RuntimeException('deprecated since 2.0, Use resolveHost() instead.');
55
    }
56
57
    /**
58
     * @deprecated
59
     * @codeCoverageIgnore
60
     */
61
    public static function addEndPoint()
62
    {
63
        throw new RuntimeException('deprecated since 2.0, Use addHost() instead.');
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @param string  $domain
69
     *
70
     * @return string
71
     * @throws ClientException
72
     * @throws ServerException
73
     */
74 14
    public static function resolveHost(Request $request, $domain = 'location.aliyuncs.com')
75
    {
76 14
        $locationService = new static($request);
77 14
        $product         = $locationService->request->product;
78 14
        $regionId        = $locationService->request->realRegionId();
79
80 14
        if (!isset(self::$hosts[$product][$regionId])) {
81 11
            self::$hosts[$product][$regionId] = self::getResult($locationService, $domain);
82 6
        }
83
84 9
        return self::$hosts[$product][$regionId];
85
    }
86
87
    /**
88
     * @param static $locationService
89
     * @param string $domain
90
     *
91
     * @return string
92
     * @throws ClientException
93
     * @throws ServerException
94
     */
95 11
    private static function getResult($locationService, $domain)
96
    {
97 11
        $locationRequest = new LocationServiceRequest($locationService->request, $domain);
98
99 11
        $result = $locationRequest->request();
100
101 8
        if (!isset($result['Endpoints']['Endpoint'][0]['Endpoint'])) {
102 2
            throw new ClientException(
103 2
                'Not found Region ID in ' . $domain,
104
                SDK::INVALID_REGION_ID
105 2
            );
106
        }
107
108 6
        return $result['Endpoints']['Endpoint'][0]['Endpoint'];
109
    }
110
111
    /**
112
     * @param string $product
113
     * @param string $host
114
     * @param string $regionId
115
     *
116
     * @throws ClientException
117
     */
118 9
    public static function addHost($product, $host, $regionId = self::GLOBAL_REGION)
119
    {
120 9
        ApiFilter::product($product);
121
122 7
        HttpFilter::host($host);
123
124 5
        ClientFilter::regionId($regionId);
125
126 3
        self::$hosts[$product][$regionId] = $host;
127 3
    }
128
129
    /**
130
     * Update endpoints from OSS.
131
     *
132
     * @codeCoverageIgnore
133
     * @throws Exception
134
     */
135
    public static function updateEndpoints()
136
    {
137
        $ossUrl = 'https://openapi-endpoints.oss-cn-hangzhou.aliyuncs.com/endpoints.json';
138
        $json   = \file_get_contents($ossUrl);
139
        $list   = \json_decode($json, true);
140
141
        foreach ($list['endpoints'] as $endpoint) {
142
            Config::set(
143
                "endpoints.{$endpoint['service']}.{$endpoint['regionid']}",
144
                \strtolower($endpoint['endpoint'])
145
            );
146
        }
147
    }
148
}
149