Test Failed
Push — master ( 9d73a2...14c4f6 )
by hugh
06:42
created

SyncConfig   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A syncConfig() 0 27 3
A hashContents() 0 7 2
A handle() 0 13 3
1
<?php
2
3
namespace HughCube\ACMClient\Commands;
4
5
use HughCube\ACMClient\ACM;
6
use HughCube\ACMClient\Client;
7
use Illuminate\Console\Command;
8
9
class SyncConfig extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'acm:sync-config
17
                            {dataId : Configured ID }
18
                            {file   : Configure saved file path } 
19
                            {--name=default : Name of the ACM client, default "default" } 
20
                            {--repeat=-1 : Number of repeated checks, -1 means unlimited, default "-1" } 
21
                            {--interval=1000 : Every check interval MS, default 1000 ms }';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Save the configuration of pull ACM to a file';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return void
34
     * @throws \Exception
35
     */
36
    public function handle()
37
    {
38
        $repeat = $this->option("repeat");
39
        $interval = $this->option("interval");
40
41
        $runCount = 0;
42
        do {
43
44
            $this->syncConfig();
45
            $runCount++;
46
            usleep($interval * 1000);
47
48
        } while (-1 == $repeat || $runCount < $repeat);
49
    }
50
51
    /**
52
     * @return bool
53
     * @throws \Exception
54
     */
55
    protected function syncConfig()
56
    {
57
        $dataId = $this->argument("dataId");
58
        $file = $this->argument('file');
59
60
        $connection = $this->option('name');
61
62
        /** @var Client $client */
63
        $client = ACM::connection($connection);
64
65
        $localContents = null;
66
        if (is_file($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type string[]; however, parameter $filename of is_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        if (is_file(/** @scrutinizer ignore-type */ $file)) {
Loading history...
67
            $localContents = file_get_contents($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type string[]; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

67
            $localContents = file_get_contents(/** @scrutinizer ignore-type */ $file);
Loading history...
68
        }
69
        $localHash = $this->hashContents($localContents);
70
71
        $remoteContents = $client->read($dataId);
0 ignored issues
show
Bug introduced by
It seems like $dataId can also be of type null and string[]; however, parameter $dataId of HughCube\ACMClient\Client::read() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

71
        $remoteContents = $client->read(/** @scrutinizer ignore-type */ $dataId);
Loading history...
72
        $remoteHash = $this->hashContents($remoteContents);
73
74
        if ($localHash === $remoteHash) {
75
            $this->line(sprintf("%s <info>文件无需同步, hash: %s</info>", date('Y-m-d H:i:s'), $remoteHash));
76
        } else {
77
            file_put_contents($file, $remoteContents, LOCK_EX);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type string[]; however, parameter $filename of file_put_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

77
            file_put_contents(/** @scrutinizer ignore-type */ $file, $remoteContents, LOCK_EX);
Loading history...
78
            $this->line(sprintf("%s <info>文件同步成功, hash: %s</info>", date('Y-m-d H:i:s'), $remoteHash));
79
        }
80
81
        return true;
82
    }
83
84
    protected function hashContents($contents)
85
    {
86
        if (null === $contents) {
87
            return null;
88
        }
89
90
        return md5($contents) . '-' . crc32($contents);;
91
    }
92
}
93