Skip to content

Commit

Permalink
feat: add address repository builder query
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed May 12, 2024
1 parent d1fd272 commit b51cb0b
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

enum OnOfficeResourceType: string
{
case Address = 'address';
case Estate = 'estate';
case Fields = 'fields';
case File = 'file';
Expand Down
19 changes: 19 additions & 0 deletions src/Facades/AddressRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades;

use Illuminate\Support\Facades\Facade;
use Katalam\OnOfficeAdapter\Query\AddressBuilder;

/**
* @see \Katalam\OnOfficeAdapter\Repositories\AddressRepository
*
* @method AddressBuilder query()
*/
class AddressRepository extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Katalam\OnOfficeAdapter\Repositories\AddressRepository::class;
}
}
95 changes: 95 additions & 0 deletions src/Query/AddressBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Katalam\OnOfficeAdapter\Query;

use Illuminate\Support\Collection;
use Katalam\OnOfficeAdapter\Enums\OnOfficeAction;
use Katalam\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Katalam\OnOfficeAdapter\Exceptions\OnOfficeException;
use Katalam\OnOfficeAdapter\Services\OnOfficeService;

class AddressBuilder extends Builder
{
public function __construct(
private readonly OnOfficeService $onOfficeService,
) {
}

public function get(): Collection
{
$columns = $this->columns;
$filter = $this->getFilters();
$listLimit = $this->limit;
$listOffset = $this->offset;
$orderBy = $this->getOrderBy();

$sortBy = data_get(array_keys($orderBy), 0);
$sortOrder = data_get($orderBy, 0);

return $this->onOfficeService->requestAll(/**
* @throws OnOfficeException
*/ function (int $pageSize, int $offset) use ($sortOrder, $sortBy, $filter, $columns) {
return $this->onOfficeService->requestApi(
OnOfficeAction::Read,
OnOfficeResourceType::Address,
parameters: [
OnOfficeService::DATA => $columns,
OnOfficeService::FILTER => $filter,
OnOfficeService::LISTLIMIT => $pageSize,
OnOfficeService::LISTOFFSET => $offset,
OnOfficeService::SORTBY => $sortBy,
OnOfficeService::SORTORDER => $sortOrder,
]
);
}, pageSize: $listLimit, offset: $listOffset);
}

/**
* @throws OnOfficeException
*/
public function first(): array
{
$columns = $this->columns;
$filter = $this->getFilters();
$listLimit = $this->limit;
$listOffset = $this->offset;
$orderBy = $this->getOrderBy();

$sortBy = data_get(array_keys($orderBy), 0);
$sortOrder = data_get($orderBy, 0);

$response = $this->onOfficeService->requestApi(
OnOfficeAction::Read,
OnOfficeResourceType::Address,
parameters: [
OnOfficeService::DATA => $columns,
OnOfficeService::FILTER => $filter,
OnOfficeService::LISTLIMIT => $listLimit,
OnOfficeService::LISTOFFSET => $listOffset,
OnOfficeService::SORTBY => $sortBy,
OnOfficeService::SORTORDER => $sortOrder,
]
);

return $response->json('response.results.0.data.records.0');
}

/**
* @throws OnOfficeException
*/
public function find(int $id): array
{
$columns = $this->columns;

$response = $this->onOfficeService->requestApi(
OnOfficeAction::Get,
OnOfficeResourceType::Address,
$id,
parameters: [
OnOfficeService::DATA => $columns,
]
);

return $response->json('response.results.0.data.records.0');
}
}
22 changes: 22 additions & 0 deletions src/Repositories/AddressRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Katalam\OnOfficeAdapter\Repositories;

use Katalam\OnOfficeAdapter\Query\AddressBuilder;
use Katalam\OnOfficeAdapter\Services\OnOfficeService;

readonly class AddressRepository
{
public function __construct(
private OnOfficeService $onOfficeService,
) {
}

/**
* Returns a new address builder instance.
*/
public function query(): AddressBuilder
{
return new AddressBuilder($this->onOfficeService);
}
}
2 changes: 2 additions & 0 deletions src/Services/OnOfficeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class OnOfficeService

public const SORTBY = 'sortby';

public const SORTORDER = 'sortorder';

private string $token;

private string $secret;
Expand Down
25 changes: 25 additions & 0 deletions tests/Query/AddressBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Support\Facades\Http;
use Katalam\OnOfficeAdapter\Facades\AddressRepository;
use Katalam\OnOfficeAdapter\Tests\Stubs\ReadAddressResponse;

it('works', function () {
Http::preventStrayRequests();
Http::fake([
'*' => Http::sequence([
// Each response will have 600 estates to simulate pagination
ReadAddressResponse::make(addressId: 1, count: 1500),
ReadAddressResponse::make(addressId: 2, count: 1500),
ReadAddressResponse::make(addressId: 3, count: 1500),
]),
]);

$addresses = AddressRepository::query()
->get();

expect($addresses)
->toHaveCount(3)
->and($addresses->first()['id'])->toBe(1)
->and($addresses->last()['id'])->toBe(3);
});
43 changes: 0 additions & 43 deletions tests/Query/EstateBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Katalam\OnOfficeAdapter\Facades\EstateRepository;
use Katalam\OnOfficeAdapter\Tests\Stubs\ReadEstateResponse;
Expand All @@ -24,45 +23,3 @@
->and($estates->first()['id'])->toBe(1)
->and($estates->last()['id'])->toBe(3);
});

it('works with difficult request', function () {
Http::preventStrayRequests();
Http::fake();

EstateRepository::query()
->select('Id')
->where('status', 1)
->where('kaufpreis', '<', 30_000)
->orderBy('kaufpreis')
->orderBy('warmmiete')
->get();

Http::assertSent(/**
* @throws JsonException
*/ static function (Request $request) {
$body = json_decode($request->body(), true, 512, JSON_THROW_ON_ERROR);
$actual = data_get($body, 'request.actions.0.parameters');

expect($actual)
->toBeArray()
->and(data_get($actual, 'data.0'))->toBe('Id')
->and(data_get($actual, 'filter'))->toBe([
'status' => [
'op' => '=',
'val' => 1,
],
'kaufpreis' => [
'op' => '<',
'val' => 30_000,
],
])
->and(data_get($actual, 'sortby'))->toBe([
'kaufpreis' => 'ASC',
'warmmiete' => 'ASC',
])
->and(data_get($actual, 'listlimit'))->toBe(500)
->and(data_get($actual, 'listoffset'))->toBe(0);

return true;
});
});
61 changes: 61 additions & 0 deletions tests/Stubs/ReadAddressResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Katalam\OnOfficeAdapter\Tests\Stubs;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Facades\Http;

class ReadAddressResponse
{
public static function make(array $data = [], int $addressId = 3729, int $count = 1): PromiseInterface
{
return Http::response(self::getBody($data, $addressId, $count));
}

private static function getBody(array $data, int $addressId, int $count): array
{
return array_merge_recursive([
'status' => [
'code' => 200,
'errorcode' => 0,
'message' => 'OK',
],
'response' => [
'results' => [
[
'actionid' => 'urn:onoffice-de-ns:smart:2.5:smartml:action:read',
'resourceid' => '',
'resourcetype' => 'address',
'cacheable' => true,
'identifier' => '',
'data' => [
'meta' => [
'cntabsolute' => $count,
],
'records' => [
[
'id' => $addressId,
'type' => 'address',
'elements' => [
'id' => (string) $addressId,
'Briefanrede' => 'Herr',
'Vorname' => 'Max',
'Name' => 'Mustermann',
'Land' => 'Deutschland',
'Ort' => 'Musterstadt',
'Plz' => '12345',
'Strasse' => 'Musterstraße',
],
],
],
'status' => [
'errorcode' => 0,
'message' => 'OK',
],
],
],
],
],
], $data);
}
}

0 comments on commit b51cb0b

Please sign in to comment.