diff --git a/src/FHIR/Exception/FHIRException.php b/src/Exception/FHIR/FHIRException.php similarity index 68% rename from src/FHIR/Exception/FHIRException.php rename to src/Exception/FHIR/FHIRException.php index 33c8bde..1f1a154 100644 --- a/src/FHIR/Exception/FHIRException.php +++ b/src/Exception/FHIR/FHIRException.php @@ -1,6 +1,6 @@ encounter->addDiagnosis($condition_uuid, $condition->condition['code']['coding'][0]['code'], '', true); - if(!isset($this->bundle['entry'][0])){ + if (!isset($this->bundle['entry'][0])) { $this->bundle['entry'][0] = [ 'fullUrl' => 'urn:uuid:' . $this->encounter_id, 'resource' => '', diff --git a/src/FHIR/Condition.php b/src/FHIR/Condition.php index 0d34255..2113037 100644 --- a/src/FHIR/Condition.php +++ b/src/FHIR/Condition.php @@ -4,7 +4,7 @@ use Satusehat\Integration\OAuth2Client; use Satusehat\Integration\Terminology\Icd10; -use Satusehat\Integration\FHIR\Exception\FHIRException; +use Satusehat\Integration\Exception\FHIR\FHIRException; class Condition extends OAuth2Client { diff --git a/src/FHIR/Observation.php b/src/FHIR/Observation.php index e258753..9261968 100644 --- a/src/FHIR/Observation.php +++ b/src/FHIR/Observation.php @@ -4,7 +4,7 @@ use Satusehat\Integration\FHIR\Enum\ObservationCategory; use Satusehat\Integration\FHIR\Enum\ObservationCode; -use Satusehat\Integration\FHIR\Exception\FHIRMissingProperty; +use Satusehat\Integration\Exception\FHIR\FHIRMissingProperty; use Satusehat\Integration\OAuth2Client; class Observation extends OAuth2Client @@ -141,7 +141,7 @@ public function addCode(string $code): Observation /** * Sets the subject of the observation. * - * @param string $subjectId The Satu Sehat ID of the subject. + * @param string $subjectId The SATUSEHAT ID of the subject. * @param string $name The name of the subject. * @return Observation The current observation instance. */ @@ -158,7 +158,7 @@ public function setSubject(string $subjectId, string $name): Observation /** * Sets the performer of the observation. * - * @param string $performerId The Satu Sehat ID of the performer. + * @param string $performerId The SATUSEHAT ID of the performer. * @param string $name The name of the performer. * @return Observation The current observation instance. */ @@ -175,7 +175,7 @@ public function setPerformer(string $performerId, string $name) /** * Visit data where observation results are obtained * - * @param string $encounterId The Satu Sehat Encounter ID of the encounter. + * @param string $encounterId The SATUSEHAT Encounter ID of the encounter. * @param string $display The display name of the encounter. */ public function setEncounter(string $encounterId, string $display = null): Observation diff --git a/src/FHIR/Organization.php b/src/FHIR/Organization.php index 79f2d1b..2e99c36 100644 --- a/src/FHIR/Organization.php +++ b/src/FHIR/Organization.php @@ -2,7 +2,7 @@ namespace Satusehat\Integration\FHIR; -use Satusehat\Integration\FHIR\Exception\FHIRException; +use Satusehat\Integration\Exception\FHIR\FHIRException; use Satusehat\Integration\OAuth2Client; class Organization extends OAuth2Client @@ -19,7 +19,7 @@ class Organization extends OAuth2Client public function addIdentifier($organization_identifier) { - $identifier['system'] = 'http://sys-ids.kemkes.go.id/organization/'.$this->organization_id; + $identifier['system'] = 'http://sys-ids.kemkes.go.id/organization/' . $this->organization_id; $identifier['value'] = $organization_identifier; $identifier['use'] = 'official'; @@ -48,12 +48,12 @@ public function setOperationalStatus($operational_status = null) public function setPartOf($partOf = null) { - $this->organization['partOf']['reference'] = 'Organization/'.($partOf ? $partOf : $this->organization_id); + $this->organization['partOf']['reference'] = 'Organization/' . ($partOf ? $partOf : $this->organization_id); } public function setType($type = 'dept') { - if (! in_array($type, ['dept', 'prov'])) { + if (!in_array($type, ['dept', 'prov'])) { throw new FHIRException("Types of organizations currently supported : 'prov' | 'dept' "); } @@ -152,23 +152,23 @@ public function json() ]; // Identifier is required - if (! array_key_exists('identifier', $this->organization)) { + if (!array_key_exists('identifier', $this->organization)) { return 'Please use organization->addIdentifier($organization_identifier) to pass the data'; } // Name is required - if (! array_key_exists('name', $this->organization)) { + if (!array_key_exists('name', $this->organization)) { return 'Please use organization->setName($organization_name) to pass the data'; } // Set default Organization part.Of - if (! array_key_exists('partOf', $this->organization)) { + if (!array_key_exists('partOf', $this->organization)) { $this->setPartOf(); } // Set default Organization type - if (! array_key_exists('type', $this->organization)) { - $this->setType($this->organization_type); + if (!array_key_exists('type', $this->organization)) { + $this->setType(); } return json_encode($this->organization, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); diff --git a/src/FHIR/Patient.php b/src/FHIR/Patient.php index 71ac942..e7530a3 100644 --- a/src/FHIR/Patient.php +++ b/src/FHIR/Patient.php @@ -3,7 +3,7 @@ namespace Satusehat\Integration\FHIR; use Satusehat\Integration\OAuth2Client; -use Satusehat\Integration\FHIR\Exception\FHIRException; +use Satusehat\Integration\Exception\FHIR\FHIRException; class Patient extends OAuth2Client { @@ -19,16 +19,15 @@ class Patient extends OAuth2Client public function addIdentifier($identifier_type, $identifier_value) { - if($identifier_type !== 'nik' && $identifier_type !== 'nik-ibu'){ + if ($identifier_type !== 'nik' && $identifier_type !== 'nik-ibu') { throw new FHIRException("\$patient->addIdentifier error. Currently, we only support 'nik' or 'nik-ibu' usage."); } $identifier['use'] = 'official'; - $identifier['system'] = 'https://fhir.kemkes.go.id/id/'.$identifier_type; + $identifier['system'] = 'https://fhir.kemkes.go.id/id/' . $identifier_type; $identifier['value'] = $identifier_value; $this->patient['identifier'][] = $identifier; - } public function setName($patient_name) @@ -47,7 +46,6 @@ public function addTelecom($telecom_value, $telecom_system = 'phone', $telecom_u $telecom['use'] = $telecom_use; // https://www.hl7.org/fhir/valueset-contact-point-use.html $this->patient['telecom'][] = $telecom; - } public function setGender($gender) @@ -120,7 +118,7 @@ public function setMaritalStatus($marital_status, $marital_code = null, $marital * $patient->setMaritalStatus('', 'UNK', 'Unknown') reference: https://www.hl7.org/fhir/valueset-marital-status.html */ $status = strtolower($marital_status); - switch($status){ + switch ($status) { case 'unmarried': $marital_code = 'U'; $marital_display = 'Unmarried'; @@ -141,7 +139,7 @@ public function setMaritalStatus($marital_status, $marital_code = null, $marital $marital_code = 'W'; $marital_display = 'Widowed'; break; - default: + default: }; $marital['coding'] = [ @@ -155,14 +153,13 @@ public function setMaritalStatus($marital_status, $marital_code = null, $marital $marital['text'] = $marital_display; $this->patient['maritalStatus'] = $marital; - } public function setMultipleBirth($value) { - if(is_bool($value)){ + if (is_bool($value)) { $this->patient['multipleBirthBoolean'] = $value; - } else if(is_int($value)){ + } else if (is_int($value)) { $this->patient['multipleBirthInteger'] = $value; } } @@ -192,7 +189,6 @@ public function setEmergencyContact($name, $phone_number) ]; $this->patient['contact'][] = $emergency; - } public function setCommunication($code = 'id-ID', $display = 'Indonesian', bool $preferred = true) @@ -235,33 +231,32 @@ public function json() { // identifier is required - if (! array_key_exists('identifier', $this->patient)) { + if (!array_key_exists('identifier', $this->patient)) { throw new FHIRException('Please use patient->addIdentifier($identifier_type, $identifier_value) to pass the data'); } // Name is required - if (! array_key_exists('name', $this->patient)) { + if (!array_key_exists('name', $this->patient)) { throw new FHIRException('Please use patient->setName($organization_name) to pass the data'); } // Address is required - if (! array_key_exists('address', $this->patient)) { + if (!array_key_exists('address', $this->patient)) { throw new FHIRException('Please use patient->setAddress($address_detail) to pass the data'); } // Telecom is required - if(! array_key_exists('telecom', $this->patient)){ + if (!array_key_exists('telecom', $this->patient)) { throw new FHIRException('Please use patinet->addTelecom("phone_number") to pass the data'); } // Multiple birth is required - if(! array_key_exists('multipleBirthInteger', $this->patient)) { + if (!array_key_exists('multipleBirthInteger', $this->patient)) { throw new FHIRException('Please use patient->setMultipleBirth({integer/boolean}) to pass the data'); } return json_encode($this->patient, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); - } public function post() diff --git a/src/OAuth2Client.php b/src/OAuth2Client.php index 8387eda..3e56f83 100644 --- a/src/OAuth2Client.php +++ b/src/OAuth2Client.php @@ -7,6 +7,7 @@ // Guzzle HTTP Package use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Psr7\Request; +use Satusehat\Integration\Exception\Helper\OAuth2ClientException; // SATUSEHAT Model & Log use Satusehat\Integration\Models\SatusehatLog; use Satusehat\Integration\Models\SatusehatToken; @@ -62,6 +63,26 @@ public function __construct() $this->organization_id = getenv('ORGID_DEV'); } + if (empty($this->satusehat_env)) { + throw new OAuth2ClientException('SATUSEHAT environment is missing'); + } + + if (!in_array($this->satusehat_env, ['DEV', 'STG', 'PROD'])) { + throw new OAuth2ClientException('SATUSEHAT environment invalid, supported (DEV, STG, PROD). ' . $this->satusehat_env . ' given.'); + } + + if ($this->satusehat_env == 'DEV' && (empty($this->client_id) || empty($this->client_secret || empty($this->organization_id)))) { + throw new OAuth2ClientException('SATUSEHAT environment defined as DEV, but CLIENTID_DEV / CLIENTSECRET_DEV / ORGID_DEV not set'); + } + + if ($this->satusehat_env == 'STG' && (empty($this->client_id) || empty($this->client_secret || empty($this->organization_id)))) { + throw new OAuth2ClientException('SATUSEHAT environment defined as STG, but CLIENTID_STG / CLIENTSECRET_STG / ORGID_STG not set'); + } + + if ($this->satusehat_env == 'PROD' && (empty($this->client_id) || empty($this->client_secret || empty($this->organization_id)))) { + throw new OAuth2ClientException('SATUSEHAT environment defined as PROD, but CLIENTID_PROD / CLIENTSECRET_PROD / ORGID_PROD not set'); + } + $this->base_url = $this->override ? null : $this->base_url; $authEndpoint = getenv('SATUSEHAT_AUTH_ENDPOINT') ?: '/oauth2/v1'; @@ -226,7 +247,7 @@ public function get_by_nik($resource, $nik) } /** - * Get request to satu sehat master data resource + * Get request to SATUSEHAT master data resource * * @param [type] $resource * @param [type] $queryString @@ -259,7 +280,7 @@ public function ss_kfa_get($resource, $queryString) if (!empty($response) && empty($response->total)) { $id = 'Not Found'; } else { - $id = 'KFA_GET_' . $resource; + $id = 'Kfa_GET_' . $resource; } } @@ -267,7 +288,7 @@ public function ss_kfa_get($resource, $queryString) if (!empty($response) && empty($response->result)) { $id = 'Not Found'; } else { - $id = 'KFA_GET_' . $resource; + $id = 'Kfa_GET_' . $resource; } } diff --git a/src/Terminology/KFA.php b/src/Terminology/Kfa.php similarity index 66% rename from src/Terminology/KFA.php rename to src/Terminology/Kfa.php index ccd32cd..6c0d1e9 100644 --- a/src/Terminology/KFA.php +++ b/src/Terminology/Kfa.php @@ -2,15 +2,17 @@ namespace Satusehat\Integration\Terminology; -use Satusehat\Integration\FHIR\Exception\FHIRException; +use Satusehat\Integration\Exception\Terminology\TerminologyException; +use Satusehat\Integration\Exception\Terminology\TerminologyInvalidArgumentException; +use Satusehat\Integration\Exception\Terminology\TerminologyMissingArgumentException; use Satusehat\Integration\OAuth2Client; -class KFA extends OAuth2Client +class Kfa extends OAuth2Client { private array $identifier = ['kfa', 'lkpp', 'nie']; /** - * Get Detail KFA Product + * Get Detail Kfa Product * * @param string $identifier currently available stroed in : $identifier * @param string $code @@ -19,11 +21,7 @@ class KFA extends OAuth2Client public function getProduct(string $identifier, string $code) { if (!in_array($identifier, $this->identifier)) { - throw new FHIRException("Identifier currently available : " . implode("','", $this->identifier)); - } - - if (empty($code)) { - throw new FHIRException("code product required", 422); + throw new TerminologyInvalidArgumentException("Identifier currently available (" . implode(", ", $this->identifier) . "), $identifier given"); } $queryStringBuilder = [ @@ -36,7 +34,7 @@ public function getProduct(string $identifier, string $code) return $this->ss_kfa_get("products?", $queryString); } /** - * Get paginated KFA Products + * Get paginated Kfa Products * * @param string $productType currently available : 'alkes' | 'farmasi' * @param integer $page min 1 no max @@ -46,20 +44,16 @@ public function getProduct(string $identifier, string $code) */ public function getProducts(string $productType, string $keyword = null, int $page = 1, int $size = 100) { - if (empty($productType)) { - throw new FHIRException("Product type required", 422); - } - if (!in_array($productType, ['alkes', 'farmasi'])) { - throw new FHIRException("Product types of currently available for : 'alkes' | 'farmasi'", 422); + throw new TerminologyInvalidArgumentException("\$productType currently available (alkes | farmasi), $productType given."); } if ($size > 1000) { - throw new FHIRException("Maximum size record 1000/request", 422); + throw new TerminologyException("Maximum size record 1000/request, $size given."); } if ($page < 1 || $size < 1) { - throw new FHIRException("Page / Size can't be blank."); + throw new TerminologyInvalidArgumentException("Page / Size cant be blank."); } $queryStringBuilder = [