-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
PingVersionServer.php
101 lines (86 loc) · 2.76 KB
/
PingVersionServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace App\Console\Commands;
use App\Contact;
use App\Instance;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class PingVersionServer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monica:ping';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Ping version.monicahq.com to know if a new version is available';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (config('monica.check_version') == false) {
return false;
}
if (env('APP_ENV') != 'production') {
return false;
}
$instance = Instance::first();
// Prepare the json to query version.monicahq.com
$json = [
'uuid' => $instance->uuid,
'version' => $instance->current_version,
'contacts' => Contact::count(),
];
$data['uuid'] = $instance->uuid;
$data['version'] = $instance->current_version;
$data['contacts'] = Contact::all()->count();
// Send the JSON
try {
$client = new Client();
$response = $client->post(config('monica.weekly_ping_server_url'), [
'json' => $data,
]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return;
} catch (\GuzzleHttp\Exception\TransferException $e) {
return;
}
// Receive the JSON
$json = json_decode($response->getBody(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
// JSON is invalid
// The function json_last_error returns the last error occurred during the JSON encoding and decoding
return;
}
// make sure the JSON has all the fields we need
if (isset($json['latest_version']) == false or isset($json['new_version']) == false or isset($json['number_of_versions_since_user_version']) == false) {
return;
}
if ($json['latest_version'] != $instance->current_version) {
$instance->latest_version = $json['latest_version'];
$instance->latest_release_notes = $json['notes'];
$instance->number_of_versions_since_current_version = $json['number_of_versions_since_user_version'];
$instance->save();
} else {
$instance->latest_release_notes = null;
$instance->number_of_versions_since_current_version = null;
$instance->save();
}
}
}