Skip to content

Commit

Permalink
Merge pull request #5 from zimmo-be/ME-125
Browse files Browse the repository at this point in the history
ME-125: Add updatePropertyPublicationForBroker
  • Loading branch information
stefanbraspenning committed May 27, 2024
2 parents 586707e + bd3f7ce commit 308d349
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
62 changes: 59 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use RuntimeException;

use function assert;
use function http_build_query;
use function is_array;
use function is_string;
use function json_encode;

class Client
{
protected ResponseEvaluator $responseEvaluator;
private ClientInterface $httpClient;
private RequestFactoryInterface $requestFactory;
private ?StreamFactoryInterface $streamFactory;

public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, ResponseEvaluator $responseEvaluator)
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, ResponseEvaluator $responseEvaluator, ?StreamFactoryInterface $streamFactory = null)
{
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->responseEvaluator = $responseEvaluator;
$this->streamFactory = $streamFactory;
}

/**
Expand Down Expand Up @@ -128,6 +134,26 @@ public function getPropertyForBroker(string $brokerId, int $propertyId, AccessTo
return $evaluatedResponse;
}

public function updatePropertyPublicationForBroker(
string $brokerId,
int $propertyId,
string $status,
?string $link,
AccessToken $accessToken
): void {
$response = $this->sendPutCall(
'/api/brokers/' . $brokerId . '/real-estate/properties/' . $propertyId . '/publication',
$this->getAuthorizationHeader($accessToken->getAccessToken()),
[],
[
'status' => $status,
'link' => $link,
]
);

$this->responseEvaluator->evaluateResponse($response);
}

/**
* @return mixed[]
*
Expand Down Expand Up @@ -200,18 +226,48 @@ private function sendGetCall(string $uri, array $headers = [], array $queryParam

/**
* @param array<string, string> $headers
* @param string[] $queryParams
*
* @throws ClientExceptionInterface
*/
private function send(string $method, string $uri, array $headers = []): ResponseInterface
private function sendPutCall(string $uri, array $headers = [], array $queryParams = [], mixed $content = null): ResponseInterface
{
return $this->send(
'PUT',
$this->createUrl($uri, $queryParams),
$headers,
$content
);
}

/**
* @param array<string, string> $headers
*
* @throws ClientExceptionInterface
*/
private function send(string $method, string $uri, array $headers = [], mixed $content = null): ResponseInterface
{
$request = $this->requestFactory->createRequest($method, $uri)
->withHeader('Content-Type', 'application/problem+json');
->withHeader('Content-Type', 'application/json');

foreach ($headers as $headerName => $headerValue) {
$request = $request->withHeader($headerName, $headerValue);
}

if ($content) {
if (! $this->streamFactory) {
throw new RuntimeException('StreamFactory is required to send content');
}

$jsonContent = json_encode($content);
assert(is_string($jsonContent));

$stream = $this->streamFactory->createStream();
$stream->write($jsonContent);

$request = $request->withBody($stream);
}

return $this->httpClient->sendRequest($request);
}

Expand Down
3 changes: 3 additions & 0 deletions src/JsonResponseEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function evaluateResponse(ResponseInterface $response): mixed
case StatusCode::OK:
return $this->handleResponseOk($response);

case StatusCode::NO_CONTENT:
return null;

case StatusCode::BAD_REQUEST:
$this->handleResponseBadRequest($response);
break;
Expand Down
11 changes: 11 additions & 0 deletions src/MiepClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public function getPropertyForBroker(string $brokerId, int $propertyId): array
}
}

public function updatePropertyPublicationForBroker(string $brokerId, int $propertyId, string $status, ?string $link): void
{
try {
$this->client->updatePropertyPublicationForBroker($brokerId, $propertyId, $status, $link, $this->getAccessToken());
} catch (Unauthorized $e) {
$this->resetAccessToken();

$this->client->updatePropertyPublicationForBroker($brokerId, $propertyId, $status, $link, $this->getAccessToken());
}
}

/**
* @return mixed[]
*
Expand Down
12 changes: 12 additions & 0 deletions src/PublicationStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MaxImmo\ExternalParties;

interface PublicationStatus
{
public const CREATED = 'CREATED';
public const UPDATED = 'UPDATED';
public const REMOVED = 'REMOVED';
}
12 changes: 6 additions & 6 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testGetBrokersShouldPerformRequestUsingCorrectParameters(): void
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Bearer access_token_test'],
)
->willReturn($this->request);
Expand All @@ -77,7 +77,7 @@ public function testGetRealEstateListForBrokerShouldPerformRequestUsingCorrectPa
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Bearer access_token_test'],
)
->willReturn($this->request);
Expand All @@ -101,7 +101,7 @@ public function testGetPropertyForBrokerShouldPerformRequestUsingCorrectParamete
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Bearer access_token_test'],
)
->willReturn($this->request);
Expand All @@ -125,7 +125,7 @@ public function testGetProjectForBrokerShouldPerformRequestUsingCorrectParameter
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Bearer access_token_test'],
)
->willReturn($this->request);
Expand All @@ -150,7 +150,7 @@ public function testGetAccessTokenShouldPerformRequestUsingCorrectParameters():
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Basic authorization'],
)
->willReturn($this->request);
Expand All @@ -170,7 +170,7 @@ public function testGetAccessTokenShouldReturnAccessTokenIfExists(): void
->expects($this->exactly(2))
->method('withHeader')
->withConsecutive(
['Content-Type', 'application/problem+json'],
['Content-Type', 'application/json'],
['Authorization', 'Basic authorization'],
)
->willReturn($this->request);
Expand Down

0 comments on commit 308d349

Please sign in to comment.