Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
75.00% |
9 / 12 |
| SyncApiClient | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
5.39 | |
75.00% |
9 / 12 |
| __construct | |
0.00% |
0 / 1 |
2.06 | |
75.00% |
6 / 8 |
|||
| getCredentials | |
0.00% |
0 / 1 |
3.14 | |
75.00% |
3 / 4 |
|||
| 1 | <?php |
| 2 | namespace CeculaSyncApiClient; |
| 3 | |
| 4 | use Exception; |
| 5 | |
| 6 | class SyncApiClient |
| 7 | { |
| 8 | private static string $credentialSource = ".ceculasync.json"; |
| 9 | private object $credentials; |
| 10 | |
| 11 | protected object $apiManager; |
| 12 | protected array $requestHeader; |
| 13 | |
| 14 | public function __construct() |
| 15 | { |
| 16 | $this->apiManager = json_decode(APIManager::getInstance()->getEndpointData()); |
| 17 | |
| 18 | try { |
| 19 | $this->credentials = json_decode($this->getCredentials()); |
| 20 | } catch (Exception $e) { |
| 21 | printf("%s<br />%s", $e->getMessage(), $e->getCode()); |
| 22 | } |
| 23 | |
| 24 | $this->requestHeader = [ |
| 25 | 'content-type' => 'application/json', |
| 26 | 'authorization' => sprintf("Bearer %s", $this->credentials->apiKey) |
| 27 | ]; |
| 28 | |
| 29 | |
| 30 | } |
| 31 | |
| 32 | private function getCredentials(): string |
| 33 | { |
| 34 | $pathway = strstr(__DIR__, 'vendor/cecula/sync-api-client') ? "/../../../../" : "/../"; |
| 35 | if (!file_exists(__DIR__.$pathway.self::$credentialSource)) { |
| 36 | throw new Exception("Credential File not found. Please create file .ceculasync.json at your application root folder. Kindly refer to readme file for guide.", 404); |
| 37 | } else { |
| 38 | return file_get_contents(__DIR__.$pathway.self::$credentialSource); |
| 39 | } |
| 40 | } |
| 41 | } |