microserviciile de register, login finalizate
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace app\external_requests;
|
||||
|
||||
use app\exceptions\GenericErrorException;
|
||||
use CurlHandle;
|
||||
|
||||
class CurlRequest
|
||||
{
|
||||
private string $url;
|
||||
private string $request;
|
||||
private array $headers;
|
||||
private ?array $body;
|
||||
private bool $isReturnable;
|
||||
|
||||
protected function __construct(string $url, string $request, array $headers, ?array $body, bool $isReturnable)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->request = $request;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
$this->isReturnable = $isReturnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GenericErrorException
|
||||
*/
|
||||
public function makeRequest(): string
|
||||
{
|
||||
$curl = curl_init();
|
||||
$this->setCurlBody($curl);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if(!$response){
|
||||
throw new GenericErrorException();
|
||||
}
|
||||
if(curl_errno($curl)){
|
||||
echo curl_error($curl);
|
||||
die;
|
||||
}
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function setCurlBody(CurlHandle &$curl): void
|
||||
{
|
||||
curl_setopt($curl, CURLOPT_URL, $this->url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
|
||||
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->request);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, $this->isReturnable);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
|
||||
curl_setopt($curl,CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this->body));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\external_requests;
|
||||
|
||||
use app\exceptions\InvalidCurlStructureException;
|
||||
|
||||
class CurlRequestBuilder extends CurlRequest
|
||||
{
|
||||
private ?string $url = NULL;
|
||||
private ?string $request = NULL;
|
||||
private ?array $headers = NULL;
|
||||
private ?array $body = NULL;
|
||||
private bool $isReturnable = false;
|
||||
|
||||
public function __construct(){}
|
||||
|
||||
public function setUrl(?string $url): void{$this->url = $url;}
|
||||
public function setHeaders(?array $headers): void{$this->headers = $headers;}
|
||||
public function setRequest(?string $request): void{$this->request = $request;}
|
||||
public function setBody(?array $body): void{$this->body = $body;}
|
||||
public function setIsReturnable(?bool $isReturnable): void{$this->isReturnable = $isReturnable;}
|
||||
|
||||
private function areMainParametersNotSet(): bool{
|
||||
return $this->url == NULL || $this->request == NULL || $this->headers == NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidCurlStructureException
|
||||
*/
|
||||
public function getCurlRequestObject(): CurlRequest{
|
||||
if($this->areMainParametersNotSet()){
|
||||
throw new InvalidCurlStructureException();
|
||||
}
|
||||
|
||||
return new CurlRequest($this->url, $this->request, $this->headers, $this->body, $this->isReturnable);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user