Completed
Push — master ( 03387d...13488e )
by Biao
04:45
created

Client   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 128
c 5
b 0
f 0
dl 0
loc 205
rs 5.5199
wmc 56

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 7
A attachCommandOptions() 0 10 1
A putCommandOptionsToEnv() 0 14 2
B startWatchNotification() 0 34 10
A stopWatchNotification() 0 3 1
B pullAllAndSave() 0 28 8
B pullBatch() 0 23 8
A pullAll() 0 3 1
B createFromEnv() 0 15 8
C createFromCommandOptions() 0 15 10

How to fix   Complexity   

Complex Class

Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.

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'      => ($clientIp = (string)getenv('APOLLO_CLIENT_IP')) !== '' ? $clientIp : null,
79
            'pull_timeout'   => ($pullTimeout = (int)getenv('APOLLO_PULL_TIMEOUT')) > 0 ? $pullTimeout : null,
80
            'backup_old_env' => ($backupOldEnv = (bool)getenv('APOLLO_BACKUP_OLD_ENV')) ? $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'] !== '' ? $options['apollo-cluster'] : null,
94
            'namespaces'     => isset($options['apollo-namespaces']) && $options['apollo-namespaces'] !== '' ? $options['apollo-namespaces'] : null,
95
            'client_ip'      => isset($options['apollo-client-ip']) && $options['apollo-client-ip'] !== '' ? $options['apollo-client-ip'] : null,
96
            'pull_timeout'   => isset($options['apollo-pull-timeout']) ? (int)$options['apollo-pull-timeout'] : null,
97
            'backup_old_env' => isset($options['apollo-backup-old-env']) ? (bool)$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
                $json = json_decode($response['body'], true);
127
                if (is_array($json)) {
128
                    $configs[$namespace] = $json;
129
                    $this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey'];
130
                }
131
            } elseif ($response['statusCode'] === 304) {
132
                // ignore 304
133
            }
134
135
        }
136
        return $configs;
137
    }
138
139
    public function pullAll($withReleaseKey = false, array $options = [])
140
    {
141
        return $this->pullBatch($this->namespaces, $withReleaseKey, $options);
142
    }
143
144
    public function pullAllAndSave($filepath, array $options = [])
145
    {
146
        $all = $this->pullAll(false, $options);
147
        if (count($all) !== count($this->namespaces)) {
148
            $lackNamespaces = array_diff($this->namespaces, array_keys($all));
149
            throw new \RuntimeException('Missing Apollo configurations for namespaces ' . implode(',', $lackNamespaces));
150
        }
151
        $configs = [];
152
        foreach ($all as $namespace => $config) {
153
            $configs[] = '# Namespace: ' . $config['namespaceName'];
154
            ksort($config['configurations']);
155
            foreach ($config['configurations'] as $key => $value) {
156
                $configs[] = sprintf('%s=%s', $key, $value);
157
            }
158
        }
159
        if (empty($configs)) {
160
            throw new \RuntimeException('Empty Apollo configuration list');
161
        }
162
        if ($this->backupOldEnv && file_exists($filepath)) {
163
            rename($filepath, $filepath . '.' . date('YmdHis'));
164
        }
165
        $fileContent = implode(PHP_EOL, $configs);
166
        if (Context::inCoroutine()) {
167
            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

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