Passed
Push — master ( 5cf1dc...0f3e3b )
by Biao
04:46
created

Apollo::__construct()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
nc 32
nop 1
dl 0
loc 20
rs 8.8333
c 4
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Components\Apollo;
4
5
use Hhxsv5\LaravelS\Components\HttpClient\SimpleHttpTrait;
6
use Hhxsv5\LaravelS\Swoole\Coroutine\Context;
7
use Swoole\Coroutine;
8
9
class Apollo
10
{
11
    use SimpleHttpTrait;
0 ignored issues
show
introduced by
The trait Hhxsv5\LaravelS\Componen...pClient\SimpleHttpTrait requires some properties which are not provided by Hhxsv5\LaravelS\Components\Apollo\Apollo: $statusCode, $body, $errMsg, $errCode, $headers
Loading history...
12
13
    protected $server;
14
    protected $appId;
15
    protected $cluster     = 'default';
16
    protected $namespaces  = ['application'];
17
    protected $clientIp;
18
    protected $pullTimeout = 5;
19
    protected $keepOldEnv  = false;
20
21
    protected $releaseKeys   = [];
22
    protected $notifications = [];
23
24
    protected $watching = true;
25
26
    public function __construct(array $settings)
27
    {
28
        $this->server = $settings['server'];
29
        $this->appId = $settings['app_id'];
30
        if (isset($settings['cluster'])) {
31
            $this->cluster = $settings['cluster'];
32
        }
33
        if (isset($settings['namespaces'])) {
34
            $this->namespaces = $settings['namespaces'];
35
        }
36
        if (isset($settings['client_ip'])) {
37
            $this->clientIp = $settings['client_ip'];
38
        } else {
39
            $this->clientIp = current(swoole_get_local_ip()) ?: null;
40
        }
41
        if (isset($settings['pull_timeout'])) {
42
            $this->pullTimeout = $settings['pull_timeout'];
43
        }
44
        if (isset($settings['keep_old_env'])) {
45
            $this->keepOldEnv = $settings['keep_old_env'];
46
        }
47
    }
48
49
    public static function createFromEnv()
50
    {
51
        $envs = getenv();
52
        if (!isset($envs['APOLLO_SERVER'], $envs['APOLLO_APP_ID'])) {
53
            throw new \InvalidArgumentException('Missing environment variable APOLLO_SERVER & APOLLO_APP_ID');
54
        }
55
        $options = [
56
            'server'       => $envs['APOLLO_SERVER'],
57
            'app_id'       => $envs['APOLLO_APP_ID'],
58
            'cluster'      => isset($envs['APOLLO_CLUSTER']) ? $envs['APOLLO_CLUSTER'] : null,
59
            'namespaces'   => isset($envs['APOLLO_NAMESPACES']) ? explode(',', $envs['APOLLO_NAMESPACES']) : null,
60
            'client_ip'    => isset($envs['APOLLO_CLIENT_IP']) ? $envs['APOLLO_CLIENT_IP'] : null,
61
            'pull_timeout' => isset($envs['APOLLO_PULL_TIMEOUT']) ? $envs['APOLLO_PULL_TIMEOUT'] : null,
62
            'keep_old_env' => isset($envs['APOLLO_KEEP_OLD_ENV']) ? $envs['APOLLO_KEEP_OLD_ENV'] : false,
63
        ];
64
        return new static($options);
65
    }
66
67
68
    public function pullBatch(array $namespaces, $withReleaseKey = false, array $options = [])
69
    {
70
        $configs = [];
71
        $uri = sprintf('%s/configs/%s/%s/', $this->server, $this->appId, $this->cluster);
72
        foreach ($namespaces as $namespace) {
73
            $url = $uri . $namespace . '?' . http_build_query([
74
                    'releaseKey' => $withReleaseKey && isset($this->releaseKeys[$namespace]) ? $this->releaseKeys[$namespace] : null,
75
                    'ip'         => $this->clientIp,
76
                ]);
77
            $timeout = isset($options['timeout']) ? $options['timeout'] : $this->pullTimeout;
78
            $response = $this->httpGet($url, compact('timeout'));
79
            if ($response['statusCode'] === 200) {
80
                $configs[$namespace] = json_decode($response['body'], true);
81
                $this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey'];
82
            } elseif ($response['statusCode'] === 304) {
83
                // ignore 304
84
            }
85
86
        }
87
        return $configs;
88
    }
89
90
    public function pullAll($withReleaseKey = false, array $options = [])
91
    {
92
        return $this->pullBatch($this->namespaces, $withReleaseKey, $options);
93
    }
94
95
    public function pullAllAndSave($filepath, array $options = [])
96
    {
97
        $all = $this->pullAll(false, $options);
98
        if (count($all) !== count($this->namespaces)) {
99
            throw new \RuntimeException('Incomplete Apollo configuration list');
100
        }
101
        $configs = [];
102
        foreach ($all as $namespace => $config) {
103
            $configs[] = '# Namespace: ' . $config['namespaceName'];
104
            ksort($config['configurations']);
105
            foreach ($config['configurations'] as $key => $value) {
106
                $configs[] = sprintf('%s=%s', $key, $value);
107
            }
108
        }
109
        if (empty($configs)) {
110
            throw new \RuntimeException('Empty Apollo configuration list');
111
        }
112
        if ($this->keepOldEnv && file_exists($filepath)) {
113
            rename($filepath, $filepath . '.' . date('YmdHis'));
114
        }
115
        $fileContent = implode(PHP_EOL, $configs);
116
        if (Context::inCoroutine()) {
117
            Coroutine::writeFile($filepath, $fileContent);
0 ignored issues
show
Bug introduced by
The call to Swoole\Coroutine::writeFile() has too few arguments starting with flags. ( Ignorable by Annotation )

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

117
            Coroutine::/** @scrutinizer ignore-call */ 
118
                       writeFile($filepath, $fileContent);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
118
        } else {
119
            file_put_contents($filepath, $fileContent);
120
        }
121
        return $configs;
122
    }
123
124
    public function startWatchNotification(callable $callback, array $options = [])
125
    {
126
        if (!isset($options['timeout']) || $options['timeout'] < 60) {
127
            $options['timeout'] = 70;
128
        }
129
        $this->watching = true;
130
        $this->notifications = [];
131
        foreach ($this->namespaces as $namespace) {
132
            $this->notifications[$namespace] = ['namespaceName' => $namespace, 'notificationId' => -1];
133
        }
134
        while ($this->watching) {
135
            $url = sprintf('%s/notifications/v2?%s',
136
                $this->server,
137
                http_build_query([
138
                    'appId'         => $this->appId,
139
                    'cluster'       => $this->cluster,
140
                    'notifications' => json_encode(array_values($this->notifications)),
141
                ])
142
            );
143
            $response = $this->httpGet($url, $options);
144
145
            if ($response['statusCode'] === 200) {
146
                $notifications = json_decode($response['body'], true);
147
                if (empty($notifications)) {
148
                    continue;
149
                }
150
                $callback($notifications);
151
                array_walk($notifications, function (&$notification) {
152
                    unset($notification['messages']);
153
                });
154
                $this->notifications = array_merge($this->notifications, array_column($notifications, null, 'namespaceName'));
155
            } elseif ($response['statusCode'] === 304) {
156
                // ignore 304
157
            }
158
        }
159
    }
160
161
    public function stopWatchNotification()
162
    {
163
        $this->watching = false;
164
    }
165
}
166