refactoring code, add doRequest via httpClient

This commit is contained in:
Jonathan Martz
2025-01-26 21:54:56 +01:00
parent f9911ffc29
commit 40db76d3ed
8 changed files with 693 additions and 132 deletions

View File

@@ -1,46 +0,0 @@
<?php
namespace Pb;
use PHPUnit\Framework\TestCase;
final class AuthTest extends TestCase
{
private Collection $collection;
private ?string $url;
protected function setUp(): void
{
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
$this->collection = new Collection($this->url, 'users');
}
public function testAuthUser(): void
{
$actual = $this->collection->authAsUser('support@jonathan-martz.de', 'rockt');
$expected = '{"data":{},"message":"Failed to authenticate.","status":400}';
$this->assertEquals($expected, trim($actual, PHP_EOL));
}
public function testAuthSuperUser(): void
{
$this->collection = new Collection($this->url, '_superusers');
$actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt');
$expected = '{"data":{},"message":"Failed to authenticate.","status":400}';
$this->assertEquals($expected, trim($actual, PHP_EOL));
}
public function testAuthSuperUser2(): void
{
$this->collection = new Collection($this->url, '_superusers');
$actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt');
$data = json_decode($actual,true);
$this->assertArrayHasKey('record',$data);
$this->assertArrayHasKey('token',$data);
$this->assertCount(8,$data['record']);
}
}

View File

@@ -2,6 +2,8 @@
namespace Pb;
use GuzzleHttp\Exception\GuzzleException;
/**
*
*/
@@ -27,7 +29,10 @@ class Collection
* @param string $collection
* @param string $token
*/
public function __construct(string $url, string $collection, string $token = '')
public function __construct(string $url,
string $collection,
string $token = ''
)
{
$this->url = $url;
$this->collection = $collection;
@@ -111,10 +116,18 @@ class Collection
*/
public function getFirstListItem(string $filter, array $queryParams = []): array
{
// TODO filter
$queryParams['perPage'] = 1;
$queryParams['filter'] = $filter;
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
return json_decode($response, JSON_FORCE_OBJECT)['items'][0];
$data = json_decode($response, JSON_FORCE_OBJECT);
if(empty($data['items']) || count($data['items']) < 1){
throw new exception\FirstListItemNotFoundException('First doesnt exists');
}
return $data['items'][0] ?? [];
}
/**
@@ -146,40 +159,19 @@ class Collection
*/
public function delete(string $recordId, array $queryParams = []): void
{
// TODO params ?
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
}
/**
* @param string $recordId
* @param string $url
* @param string $method
* @return bool|string
*/
public function doRequest(string $url, string $method, $bodyParams = []): string
{
// TODO move doRequestIntoService ?
// TODO replace curl with HttpClient
$ch = curl_init();
if (self::$token != '') {
$headers = array(
'Content-Type:application/json',
'Authorization: ' . self::$token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// TODO token ?
}
if ($bodyParams) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch);
curl_close($ch);
return $output;
$client = new \GuzzleHttp\Client();
$response = $client->request($method, $url,$bodyParams);
return $response->getBody()->getContents() ?? '';
}
/**
@@ -189,20 +181,16 @@ class Collection
*/
public function getOne(string $recordId, array $queryParams = []): array
{
// TODO params ?
$output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
return json_decode($output, JSON_FORCE_OBJECT);
}
/**
* @param string $email
* @param string $password
* @return void
*/
public function authAsAdmin(string $email, string $password): string
{
$bodyParams['identity'] = $email;
$bodyParams['password'] = $password;
$output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
$token = json_decode($output, true)['token'];
if ($token) {
self::$token = $token;

View File

@@ -0,0 +1,14 @@
<?php
namespace Pb\Exception;
class FirstListItemNotFoundException extends \Exception
{
/**
* @param string $string
*/
public function __construct(string $string)
{
}
}