Skip to content

Commit

Permalink
fix issues related to response (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
maztch authored Jun 17, 2024
1 parent f9130ac commit 041edbc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion samples/sign_management.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Ilovepdf\SignatureManagement;

$signatureRequest = new SignatureManagement("publickey", "secretkey");
$signatureToken = "signaturetoken";
$signatureToken = "token_requester";
$receiverToken = "receivertoken";

// Get a list of all created signature requests
Expand Down
5 changes: 3 additions & 2 deletions src/Ilovepdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Ilovepdf\Exceptions\DownloadException;
use Ilovepdf\Exceptions\ProcessException;
use Ilovepdf\Exceptions\SignatureException;
use Ilovepdf\Exceptions\StartException;
use Ilovepdf\Exceptions\TaskException;
use Ilovepdf\Exceptions\UploadException;
Expand Down Expand Up @@ -45,7 +46,7 @@ class Ilovepdf
*/
public static $apiVersion = 'v1';

const VERSION = 'php.1.2.3';
const VERSION = 'php.1.2.5';

/**
* @var string|null
Expand Down Expand Up @@ -264,7 +265,7 @@ public function sendRequest(string $method, string $endpoint, array $params = []
}
//signature exception
if(strpos($endpoint, 'signature') !== false){
throw new ProcessException($responseBody->error->type, $responseBody, $response->getStatusCode());
throw new SignatureException($responseBody->error->type, $responseBody, $response->getStatusCode());
}

if (isset($responseBody->error) && isset($responseBody->error->type)) {
Expand Down
2 changes: 1 addition & 1 deletion src/SignTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function execute()
//$response = parent::sendRequest('post', 'signature', http_build_query($body, null, '&', PHP_QUERY_RFC3986));
$response = parent::sendRequest('post', 'signature', $body,false);
try {
$this->result = json_decode($response->getBody());
$this->result = json_decode($response->getBody()->getContents());
}
catch(\Exception $e){
throw new ProcessException('Bad request');
Expand Down
46 changes: 26 additions & 20 deletions src/SignatureManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ilovepdf;

use Psr\Http\Message\ResponseInterface;
use stdClass;
use Ilovepdf\Exceptions\DownloadException;

Expand All @@ -18,6 +19,7 @@ class SignatureManagement extends Ilovepdf
* @param int $per_page
*
* @return array
* @throws \Exception
*/
public function getSignaturesList($page = 0, $per_page = 20): array
{
Expand All @@ -26,26 +28,26 @@ public function getSignaturesList($page = 0, $per_page = 20): array
"per-page" => $per_page
];
$response = parent::sendRequest("get", "signature/list", $data);
$headers = $response->headers;
$return = [
"signatures" => $response->body,
$headers = $response->getHeaders();
return [
"signatures" => $response->getBody()->getContents(),
"pagination" => [
'totalCount' => $headers && isset($headers['X-Pagination-Total-Count']) ? $headers['X-Pagination-Total-Count'] : '',
'page' => $headers && isset($headers['X-Pagination-Current-Page']) ? $headers['X-Pagination-Current-Page'] - 1 : '',
'page' => $headers && isset($headers['X-Pagination-Current-Page']) ? (int)$headers['X-Pagination-Current-Page'] - 1 : '',
'pageSize' => $headers && isset($headers['X-Pagination-Per-Page']) ? $headers['X-Pagination-Per-Page'] : ''
]
];
return $return;
}

/**
* @param string $signatureId
*
* @return stdClass
* @throws \Exception
*/
public function getSignatureStatus(string $signatureId): stdClass
{
return parent::sendRequest("get", "signature/requesterview/{$signatureId}")->body;
return json_decode(parent::sendRequest("get", "signature/requesterview/{$signatureId}")->getBody()->getContents());
}

/**
Expand Down Expand Up @@ -95,10 +97,10 @@ public function downloadSignedFiles(string $signatureId, string $pathToSave, str
*/
public function fixReceiverEmail(string $receiverTokenRequester, string $newEmail): bool
{
$body = [
$body = ['form_params' => [
"email" => $newEmail
];
$response = parent::sendRequest("put", "signature/signer/fix-email/{$receiverTokenRequester}", $body, false, true);
]];
parent::sendRequest("put", "signature/signer/fix-email/{$receiverTokenRequester}", $body, false, true);
//if above fails, it throws an exception
return true;
}
Expand All @@ -111,9 +113,9 @@ public function fixReceiverEmail(string $receiverTokenRequester, string $newEmai
*/
public function fixSignerPhone(string $signerTokenRequester, string $newPhone): bool
{
$body = [
$body = ['form_params' => [
"phone" => $newPhone
];
]];
$response = parent::sendRequest("put", "signature/signer/fix-phone/{$signerTokenRequester}", $body, false, true);
//if above fails, it throws an exception
return true;
Expand All @@ -127,7 +129,7 @@ public function fixSignerPhone(string $signerTokenRequester, string $newPhone):
public function sendReminders(string $signatureId): bool
{
$body = [];
$response = parent::sendRequest("post", "signature/sendReminder/{$signatureId}", $body, false, true);
parent::sendRequest("post", "signature/sendReminder/{$signatureId}", $body, false, true);
//if above fails, it throws an exception
return true;
}
Expand All @@ -136,11 +138,12 @@ public function sendReminders(string $signatureId): bool
* @param string $signatureId
*
* @return bool
* @throws \Exception
*/
public function voidSignature(string $signatureId): bool
{
$body = [];
$response = parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true);
parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true);
//if above fails, it throws an exception
return true;
}
Expand All @@ -150,13 +153,14 @@ public function voidSignature(string $signatureId): bool
* @param int $amountOfDays
*
* @return bool
* @throws \Exception
*/
public function increaseExpirationDays(string $signatureId, int $amountOfDays): bool
{
$body = [
"days" => $amountOfDays
];
$response = parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true);
parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true);
//if above fails, it throws an exception
return true;
}
Expand All @@ -165,10 +169,11 @@ public function increaseExpirationDays(string $signatureId, int $amountOfDays):
* @param string $receiverTokenRequester
*
* @return stdClass
* @throws \Exception
*/
public function getReceiverInfo(string $receiverTokenRequester): stdClass
{
return parent::sendRequest("get", "signature/receiver/info/{$receiverTokenRequester}")->body;
return json_decode(parent::sendRequest("get", "signature/receiver/info/{$receiverTokenRequester}")->getBody()->getContents());
}

/**
Expand All @@ -189,15 +194,16 @@ private function getExtensionFromMime(string $mime_type)
return explode("/", $mime_type)[1];
}

protected function downloadResponseToFile($response, string $pathToSave, string $filename): string
protected function downloadResponseToFile(ResponseInterface $response, string $pathToSave, string $filename): string
{
if (!is_dir($pathToSave)) {
throw new \Exception("{$pathToSave} is not a directory");
}
$mime_type = $response->headers["Content-Type"];
$filePath = "{$pathToSave}/{$filename}." . $this->getExtensionFromMime($mime_type);
if (!file_put_contents($filePath, $response->raw_body)) {
throw new DownloadException("Download success, but could not save the contents of the file to {$filePath}. Check permissions of the directory", 1, null, $response);
$mime_type = $response->getHeaders()["Content-Type"];
$filePath = "{$pathToSave}/{$filename}." . $this->getExtensionFromMime($mime_type[0]);
$fileContent = $response->getBody()->getContents();
if (!file_put_contents($filePath, $fileContent)) {
throw new DownloadException("Download success, but could not save the contents of the file to {$filePath}. Check permissions of the directory", 1, null, $fileContent);
}
return $filePath;
}
Expand Down

0 comments on commit 041edbc

Please sign in to comment.