Skip to content

Commit

Permalink
Merge pull request #44 from yudistirasd/refactor-exception
Browse files Browse the repository at this point in the history
Refactor Exception Handling with some fix
  • Loading branch information
ivanwilliammd authored Apr 10, 2024
2 parents 744b705 + a4d7e19 commit a1cce21
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Satusehat\Integration\FHIR\Exception;
namespace Satusehat\Integration\Exception\FHIR;

use Exception;

class FHIRException extends Exception
{
public function __construct($message, $code = 0, ?Exception $previous = null)
{
$message = 'FHIR Exception: '.$message;
$message = 'FHIR Exception: ' . $message;

parent::__construct($message, $code, $previous);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Satusehat\Integration\FHIR\Exception;
namespace Satusehat\Integration\Exception\FHIR;

class FHIRInvalidPropertyValue extends FHIRException
{
public function __construct($message)
{
$message = 'FHIR Invalid Property Value: '.$message;
$message = 'FHIR Invalid Property Value: ' . $message;

parent::__construct($message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Satusehat\Integration\FHIR\Exception;
namespace Satusehat\Integration\Exception\FHIR;

class FHIRMissingProperty extends FHIRException
{
public function __construct($message)
{
$message = 'FHIR Missing Property: '.$message;
$message = 'FHIR Missing Property: ' . $message;

parent::__construct($message);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/Helper/OAuth2ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Satusehat\Integration\Exception\Helper;

use Exception;

class OAuth2ClientException extends Exception
{
public function __construct($message, $code = 0, ?Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
13 changes: 13 additions & 0 deletions src/Exception/Terminology/TerminologyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Satusehat\Integration\Exception\Terminology;

use Exception;

class TerminologyException extends Exception
{
public function __construct($message, $code = 0, ?Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Satusehat\Integration\Exception\Terminology;

class TerminologyInvalidArgumentException extends TerminologyException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Satusehat\Integration\Exception\Terminology;

class TerminologyMissingArgumentException extends TerminologyException
{
}
4 changes: 2 additions & 2 deletions src/FHIR/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 Bundle extends OAuth2Client
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public function addCondition(Condition $condition)
// Membuat referensi condition di encounter
$this->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' => '',
Expand Down
2 changes: 1 addition & 1 deletion src/FHIR/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/FHIR/Observation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/FHIR/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';

Expand Down Expand Up @@ -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' ");
}

Expand Down Expand Up @@ -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);
Expand Down
29 changes: 12 additions & 17 deletions src/FHIR/Patient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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';
Expand All @@ -141,7 +139,7 @@ public function setMaritalStatus($marital_status, $marital_code = null, $marital
$marital_code = 'W';
$marital_display = 'Widowed';
break;
default:
default:
};

$marital['coding'] = [
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
25 changes: 23 additions & 2 deletions src/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,26 @@ public function __construct()
$this->organization_id = getenv('ORGID_DEV');
}

if (empty($this->satusehat_env)) {
throw new OAuth2ClientException('Satu sehat environment is missing');
}

if (!in_array($this->satusehat_env, ['DEV', 'STG', 'PROD'])) {
throw new OAuth2ClientException('Satu sehat 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('Satu sehat 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('Satu sehat 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('Satu sehat 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';
Expand Down Expand Up @@ -259,15 +280,15 @@ 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;
}
}

if ($resource == 'products?') {
if (!empty($response) && empty($response->result)) {
$id = 'Not Found';
} else {
$id = 'KFA_GET_' . $resource;
$id = 'Kfa_GET_' . $resource;
}
}

Expand Down
Loading

0 comments on commit a1cce21

Please sign in to comment.