forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CqmClient.php
142 lines (131 loc) · 3.92 KB
/
CqmClient.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace OpenEMR\Cqm;
use Exception;
use GuzzleHttp\Client;
use OpenEMR\Common\Http\HttpClient;
use OpenEMR\Common\System\System;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use function GuzzleHttp\Psr7\str;
/**
* Class CqmClient
*
* @package OpenEMR\Cqm
* @author Ken Chapple
*/
class CqmClient extends HttpClient
{
protected function getCommand(): string
{
$port = $this->port;
$node = 'node';
$cmd = $this->servicePath;
if (IS_WINDOWS) {
$cmd = "start /B $node $cmd";
return $cmd;
} else {
$command = $node;
$system = new System();
if (!$system->command_exists($node)) {
if ($system->command_exists('nodejs')) {
$command = 'nodejs';
} else {
error_log("Connection failed. Node does not appear to be installed on the system.");
throw new Exception('Connection Failed.');
}
}
$node = $command;
}
return "$node $cmd";
}
/**
* Returns the CQM service's health.
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getHealth(): array
{
try {
return json_decode(
Utils::copyToString($this->request('GET', '/health')->getBody()),
true
);
} catch (ConnectException $exception) {
return [
'uptime' => 0
];
} catch (ServerException $exception) {
return [
'uptime' => 0
];
}
}
/**
* Returns CQM Service version and dependencies lookup.
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getVersion(): array
{
return json_decode(
Utils::copyToString($this->request('GET', '/version')->getBody()),
true
);
}
/**
* Calculates a CQM measure given a QDM Patient, Measure and ValueSet
*
* @param StreamInterface $patients
* @param StreamInterface $measure
* @param StreamInterface $valueSets
* @return StreamInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function calculate(
StreamInterface $patients,
StreamInterface $measure,
StreamInterface $valueSets,
StreamInterface $options = null
) {
$patients = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$patients);
$measure = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$measure);
$valueSets = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$valueSets);
$options = (string)$options;
try {
return json_decode(
Utils::copyToString(
$this->request('POST', '/calculate', [
'form_params' => [
'patients' => $patients,
'measure' => $measure,
'valueSets' => $valueSets,
'options' => $options
]])->getBody()
),
true
);
} catch (ConnectException $exception) {
return [$exception->getMessage()];
} catch (ServerException $exception) {
return [$exception->getMessage()];
}
}
/**
* Perform a graceful shutdown of cqm-service node (express) server
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function shutdown(): array
{
return json_decode(
Utils::copyToString($this->request('GET', '/shutdown')->getBody()),
true
);
}
}