fix: Uninitialised zip and missing file size error in Native Data Loads (#7081)
[openemr.git] / src / Cqm / CqmClient.php
blob9c298007118329ee4a6f05c81d09e4f83443a9b3
1 <?php
3 namespace OpenEMR\Cqm;
5 use Exception;
6 use GuzzleHttp\Client;
7 use OpenEMR\Common\Http\HttpClient;
8 use OpenEMR\Common\System\System;
9 use Psr\Http\Message\ResponseInterface;
10 use Psr\Http\Message\StreamInterface;
11 use GuzzleHttp\Psr7\Utils;
12 use GuzzleHttp\Exception\ConnectException;
13 use GuzzleHttp\Exception\ServerException;
15 use function GuzzleHttp\Psr7\str;
17 /**
18 * Class CqmClient
20 * @package OpenEMR\Cqm
21 * @author Ken Chapple
23 class CqmClient extends HttpClient
25 protected function getCommand(): string
27 $port = $this->port;
29 $node = 'node';
30 $cmd = $this->servicePath;
32 if (IS_WINDOWS) {
33 $cmd = "start /B $node $cmd";
34 return $cmd;
35 } else {
36 $command = $node;
37 $system = new System();
38 if (!$system->command_exists($node)) {
39 if ($system->command_exists('nodejs')) {
40 $command = 'nodejs';
41 } else {
42 error_log("Connection failed. Node does not appear to be installed on the system.");
43 throw new Exception('Connection Failed.');
46 $node = $command;
49 return "$node $cmd";
52 /**
53 * Returns the CQM service's health.
55 * @return array
56 * @throws \GuzzleHttp\Exception\GuzzleException
58 public function getHealth(): array
60 try {
61 return json_decode(
62 Utils::copyToString($this->request('GET', '/health')->getBody()),
63 true
65 } catch (ConnectException $exception) {
66 return [
67 'uptime' => 0
69 } catch (ServerException $exception) {
70 return [
71 'uptime' => 0
76 /**
77 * Returns CQM Service version and dependencies lookup.
79 * @return array
80 * @throws \GuzzleHttp\Exception\GuzzleException
82 public function getVersion(): array
84 return json_decode(
85 Utils::copyToString($this->request('GET', '/version')->getBody()),
86 true
90 /**
91 * Calculates a CQM measure given a QDM Patient, Measure and ValueSet
93 * @param StreamInterface $patients
94 * @param StreamInterface $measure
95 * @param StreamInterface $valueSets
96 * @return StreamInterface
97 * @throws \GuzzleHttp\Exception\GuzzleException
99 public function calculate(
100 StreamInterface $patients,
101 StreamInterface $measure,
102 StreamInterface $valueSets,
103 StreamInterface $options = null
105 $patients = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$patients);
106 $measure = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$measure);
107 $valueSets = (string)str_replace(["\r\n", "\n", "\r"], '', (string)$valueSets);
108 $options = (string)$options;
109 try {
110 return json_decode(
111 Utils::copyToString(
112 $this->request('POST', '/calculate', [
113 'form_params' => [
114 'patients' => $patients,
115 'measure' => $measure,
116 'valueSets' => $valueSets,
117 'options' => $options
118 ]])->getBody()
120 true
122 } catch (ConnectException $exception) {
123 return [$exception->getMessage()];
124 } catch (ServerException $exception) {
125 return [$exception->getMessage()];
130 * Perform a graceful shutdown of cqm-service node (express) server
132 * @return array
133 * @throws \GuzzleHttp\Exception\GuzzleException
135 public function shutdown(): array
137 return json_decode(
138 Utils::copyToString($this->request('GET', '/shutdown')->getBody()),
139 true