Skip to content

Commit

Permalink
feat: add clientUpdate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven committed Oct 19, 2023
1 parent b80d3ab commit 60586ec
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/DataTransferObjects/Client/ClientUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\Client;

class ClientUpdate
{
public string $status;

public function __construct(object $response)
{
$this->status = $response->status;
}
}
79 changes: 68 additions & 11 deletions src/Ugyfelkartya.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Exception\GuzzleException;
use HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\Card as CardDto;
use HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\Client\ClientDelete as ClientDeleteDto;
use HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\Client\ClientUpdate as ClientUpdateDto;
use HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\ClientInfo as ClientInfoDto;
use HelixdigitalIo\NetlientUgyfelkartya\DataTransferObjects\Registration as RegistrationDto;
use HelixdigitalIo\NetlientUgyfelkartya\Traits\IsSuccessfulResponse;
Expand All @@ -18,21 +19,21 @@ class Ugyfelkartya

private Client $client;
private string $baseUrl = 'https://www.ugyfelkartya.hu/api/';

/** @var array<string, string> */
private array $headers = [
'Content-Type' => 'application/json;charset=UTF-8',
'Accept' => 'application/json',
];

public function __construct() {
public function __construct()
{
$this->client = new Client([
'base_uri' => $this->baseUrl,
'headers' => $this->headers,
'auth' => [
$_ENV['API_USERNAME'],
$_ENV['API_PASSWORD']
]
$_ENV['API_PASSWORD'],
],
]);
}

Expand All @@ -42,10 +43,10 @@ public function __construct() {
* @throws JsonException
*/
public function register(
int $storeId,
string $email,
string $firstName,
string $lastName,
int $storeId,
string $email,
string $firstName,
string $lastName,
?string $birthDate,
?string $zipCode
)
Expand All @@ -68,7 +69,7 @@ public function register(
}

$jsonResponse = (string)($this->client->post('registration', [
'json' => $body
'json' => $body,
])->getBody());

$response = $this->parseResponse($jsonResponse);
Expand Down Expand Up @@ -127,7 +128,7 @@ public function deleteClient(int $clientId, int $storeId)
'json' => [
'client_id' => $clientId,
'store_id' => $storeId,
]
],
])->getBody());

$response = $this->parseResponse($jsonResponse);
Expand All @@ -139,7 +140,63 @@ public function deleteClient(int $clientId, int $storeId)
return new ClientDeleteDto($response);
}

private function parseResponse(string $jsonResponse) {
/**
* @param int|string|null $card_type
* @return $this|ClientUpdateDto
* @throws GuzzleException
* @throws JsonException
*/
public function updateClient(
int $clientId,
?int $webstore_id = null,
?string $email = null,
?string $lastname = null,
?string $firstname = null,
?int $card_number = null,
?int $gender = null,
?string $password = null,
?bool $can_re_regist = null,
?int $newsletter = null,
?int $newslettermarketing = null,
?int $card_validity = null,
$card_type = null,
?bool $activate_card = null
)
{
$json = [
'client_id' => $clientId,
'client' => compact(
'webstore_id',
'email',
'lastname',
'firstname',
'card_number',
'gender',
'password',
'can_re_regist',
'newsletter',
'newslettermarketing',
'card_validity',
'card_type',
'activate_card'
),
];

$json['client'] = array_filter($json['client'], static fn($value) => $value !== null);

$jsonResponse = (string)($this->client->post('clientupdate', compact('json'))->getBody());

$response = $this->parseResponse($jsonResponse);

if ($response === null) {
return $this;
}

return new ClientUpdateDto($response);
}

private function parseResponse(string $jsonResponse)
{
$response = json_decode($jsonResponse, false, 512, JSON_THROW_ON_ERROR);

if (!$this->isSuccessfulResponse($response)) {
Expand Down

0 comments on commit 60586ec

Please sign in to comment.