Completed
Push — master ( 5a092f...e5a7ad )
by Biao
04:42
created

Client::pullBatch()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 7
nop 3
dl 0
loc 20
rs 8.8333
c 0
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
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class Client
12
{
13
    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\Client: $statusCode, $body, $errMsg, $errCode, $headers
Loading history...
14
15
    protected $server;
16
    protected $appId;
17
    protected $cluster      = 'default';
18
    protected $namespaces   = ['application'];
19
    protected $clientIp;
20
    protected $pullTimeout  = 5;
21
    protected $backupOldEnv = false;
22
23
    protected $releaseKeys   = [];
24
    protected $notifications = [];
25
26
    protected $watching = true;
27
28
    public function __construct(array $settings)
29
    {
30
        $this->server = $settings['server'];
31
        $this->appId = $settings['app_id'];
32
        if (isset($settings['cluster'])) {
33
            $this->cluster = $settings['cluster'];
34
        }
35
        if (isset($settings['namespaces'])) {
36
            $this->namespaces = $settings['namespaces'];
37
        }
38
        if (isset($settings['client_ip'])) {
39
            $this->clientIp = $settings['client_ip'];
40
        } else {
41
            $this->clientIp = current(swoole_get_local_ip()) ?: null;
42
        }
43
        if (isset($settings['pull_timeout'])) {
44
            $this->pullTimeout = (int)$settings['pull_timeout'];
45
        }
46
        if (isset($settings['backup_old_env'])) {
47
            $this->backupOldEnv = (bool)$settings['backup_old_env'];
48
        }
49
    }
50
51
    public static function putCommandOptionsToEnv(array $options)
52
    {
53
        $envs = [
54
            'ENABLE_APOLLO'         => !empty($options['enable-apollo']),
55
            'APOLLO_SERVER'         => $options['apollo-server'],
56
            'APOLLO_APP_ID'         => $options['apollo-app-id'],
57
            'APOLLO_CLUSTER'        => $options['apollo-cluster'],
58
            'APOLLO_NAMESPACES'     => implode(',', $options['apollo-namespaces']),
59
            'APOLLO_CLIENT_IP'      => $options['apollo-client-ip'],
60
            'APOLLO_PULL_TIMEOUT'   => $options['apollo-pull-timeout'],
61
            'APOLLO_BACKUP_OLD_ENV' => $options['apollo-backup-old-env'],
62
        ];
63
        foreach ($envs as $key => $value) {
64
            putenv("{$key}={$value}");
65
        }
66
    }
67
68
    public static function createFromEnv()
69
    {
70
        if (!getenv('APOLLO_SERVER') || !getenv('APOLLO_APP_ID')) {
71
            throw new \InvalidArgumentException('Missing environment variable APOLLO_SERVER or APOLLO_APP_ID');
72
        }
73
        $settings = [
74
            'server'         => getenv('APOLLO_SERVER'),
75
            'app_id'         => getenv('APOLLO_APP_ID'),
76
            'cluster'        => ($cluster = (string)getenv('APOLLO_CLUSTER')) !== '' ? $cluster : null,
77
            'namespaces'     => ($namespaces = (string)getenv('APOLLO_NAMESPACES')) !== '' ? explode(',', $namespaces) : null,
78
            'client_ip'      => getenv('APOLLO_CLIENT_IP') ?: null,
79
            'pull_timeout'   => ($pullTimeout = getenv('APOLLO_PULL_TIMEOUT')) !== false ? $pullTimeout : null,
80
            'backup_old_env' => ($backupOldEnv = getenv('APOLLO_BACKUP_OLD_ENV')) !== false ? $backupOldEnv : null,
81
        ];
82
        return new static($settings);
83
    }
84
85
    public static function createFromCommandOptions(array $options)
86
    {
87
        if (!isset($options['apollo-server'], $options['apollo-app-id'])) {
88
            throw new \InvalidArgumentException('Missing command option apollo-server or apollo-app-id');
89
        }
90
        $settings = [
91
            'server'         => $options['apollo-server'],
92
            'app_id'         => $options['apollo-app-id'],
93
            'cluster'        => isset($options['apollo-cluster']) ? $options['apollo-cluster'] : null,
94
            'namespaces'     => !empty($options['apollo-namespaces']) ? $options['apollo-namespaces'] : null,
95
            'client_ip'      => isset($options['apollo-client-ip']) ? $options['apollo-client-ip'] : null,
96
            'pull_timeout'   => isset($options['apollo-pull-timeout']) ? $options['apollo-pull-timeout'] : null,
97
            'backup_old_env' => isset($options['apollo-backup-old-env']) ? $options['apollo-backup-old-env'] : null,
98
        ];
99
        return new static($settings);
100
    }
101
102
    public static function attachCommandOptions(Command $command)
103
    {
104
        $command->addOption('enable-apollo', null, InputOption::VALUE_NONE, 'Whether to enable Apollo component');
105
        $command->addOption('apollo-server', null, InputOption::VALUE_OPTIONAL, 'Apollo server URL');
106
        $command->addOption('apollo-app-id', null, InputOption::VALUE_OPTIONAL, 'Apollo APP ID');
107
        $command->addOption('apollo-namespaces', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The namespace to which the APP belongs');
108
        $command->addOption('apollo-cluster', null, InputOption::VALUE_OPTIONAL, 'The cluster to which the APP belongs');
109
        $command->addOption('apollo-client-ip', null, InputOption::VALUE_OPTIONAL, 'IP of current instance');
110
        $command->addOption('apollo-pull-timeout', null, InputOption::VALUE_OPTIONAL, 'Timeout time(seconds) when pulling configuration');
111
        $command->addOption('apollo-backup-old-env', null, InputOption::VALUE_NONE, 'Whether to backup the old configuration file when updating the configuration .env file');
112
    }
113
114
    public function pullBatch(array $namespaces, $withReleaseKey = false, array $options = [])
115
    {
116
        $configs = [];
117
        $uri = sprintf('%s/configs/%s/%s/', $this->server, $this->appId, $this->cluster);
118
        foreach ($namespaces as $namespace) {
119
            $url = $uri . $namespace . '?' . http_build_query([
120
                    'releaseKey' => $withReleaseKey && isset($this->releaseKeys[$namespace]) ? $this->releaseKeys[$namespace] : null,
121
                    'ip'         => $this->clientIp,
122
                ]);
123
            $timeout = isset($options['timeout']) ? $options['timeout'] : $this->pullTimeout;
124
            $response = $this->httpGet($url, compact('timeout'));
125
            if ($response['statusCode'] === 200) {
126
                $configs[$namespace] = json_decode($response['body'], true);
127
                $this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey'];
128
            } elseif ($response['statusCode'] === 304) {
129
                // ignore 304
130
            }
131
132
        }
133
        return $configs;
134
    }
135
136
    public function pullAll($withReleaseKey = false, array $options = [])
137
    {
138
        return $this->pullBatch($this->namespaces, $withReleaseKey, $options);
139
    }
140
141
    public function pullAllAndSave($filepath, array $options = [])
142
    {
143
        $all = $this->pullAll(false, $options);
144
        if (count($all) !== count($this->namespaces)) {
145
            $lackNamespaces = array_diff($this->namespaces, array_keys($all));
146
            throw new \RuntimeException('Missing Apollo configurations for namespaces ' . implode(',', $lackNamespaces));
147
        }
148
        $configs = [];
149
        foreach ($all as $namespace => $config) {
150
            $configs[] = '# Namespace: ' . $config['namespaceName'];
151
            ksort($config['configurations']);
152
            foreach ($config['configurations'] as $key => $value) {
153
                $configs[] = sprintf('%s=%s', $key, $value);
154
            }
155
        }
156
        if (empty($configs)) {
157
            throw new \RuntimeException('Empty Apollo configuration list');
158
        }
159
        if ($this->backupOldEnv && file_exists($filepath)) {
160
            rename($filepath, $filepath . '.' . date('YmdHis'));
161
        }
162
        $fileContent = implode(PHP_EOL, $configs);
163
        if (Context::inCoroutine()) {
164
            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

164
            Coroutine::/** @scrutinizer ignore-call */ 
165
                       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...
165
        } else {
166
            file_put_contents($filepath, $fileContent);
167
        }
168
        return $configs;
169
    }
170
171
    public function startWatchNotification(callable $callback, array $options = [])
172
    {
173
        if (!isset($options['timeout']) || $options['timeout'] < 60) {
174
            $options['timeout'] = 70;
175
        }
176
        $this->watching = true;
177
        $this->notifications = [];
178
        foreach ($this->namespaces as $namespace) {
179
            $this->notifications[$namespace] = ['namespaceName' => $namespace, 'notificationId' => -1];
180
        }
181
        while ($this->watching) {
182
            $url = sprintf('%s/notifications/v2?%s',
183
                $this->server,
184
                http_build_query([
185
                    'appId'         => $this->appId,
186
                    'cluster'       => $this->cluster,
187
                    'notifications' => json_encode(array_values($this->notifications)),
188
                ])
189
            );
190
            $response = $this->httpGet($url, $options);
191
192
            if ($response['statusCode'] === 200) {
193
                $notifications = json_decode($response['body'], true);
194
                if (empty($notifications)) {
195
                    continue;
196
                }
197
                if (!empty($this->notifications) && current($this->notifications)['notificationId'] !== -1) { // Ignore the first pull
198
                    $callback($notifications);
199
                }
200
                array_walk($notifications, function (&$notification) {
201
                    unset($notification['messages']);
202
                });
203
                $this->notifications = array_merge($this->notifications, array_column($notifications, null, 'namespaceName'));
204
            } elseif ($response['statusCode'] === 304) {
205
                // ignore 304
206
            }
207
        }
208
    }
209
210
    public function stopWatchNotification()
211
    {
212
        $this->watching = false;
213
    }
214
}
215