generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add address repository builder query
- Loading branch information
Showing
8 changed files
with
225 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |