Skip to content

Commit

Permalink
feat: add imprint to settings repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed Jun 6, 2024
1 parent 7322591 commit 9d17f99
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
## main

## v0.7.4
- Added imprint to the settings repository

## v0.7.3
- Added an exception when the file is not found in a estate file query
- Added a modify method for estate files
Expand Down
1 change: 1 addition & 0 deletions src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ enum OnOfficeResourceType: string
case Regions = 'regions';
case UploadFile = 'uploadfile';
case FileRelation = 'fileRelation';
case Impressum = 'impressum';
}
2 changes: 2 additions & 0 deletions src/Facades/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Facade;
use Katalam\OnOfficeAdapter\Facades\Testing\SettingRepositoryFake;
use Katalam\OnOfficeAdapter\Query\ImprintBuilder;
use Katalam\OnOfficeAdapter\Query\RegionBuilder;
use Katalam\OnOfficeAdapter\Query\UserBuilder;

Expand All @@ -12,6 +13,7 @@
*
* @method static UserBuilder users()
* @method static RegionBuilder regions()
* @method static ImprintBuilder imprint()
*/
class SettingRepository extends Facade
{
Expand Down
9 changes: 9 additions & 0 deletions src/Facades/Testing/SettingRepositoryFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Katalam\OnOfficeAdapter\Facades\Testing;

use Katalam\OnOfficeAdapter\Query\Testing\ImprintBuilderFake;
use Katalam\OnOfficeAdapter\Query\Testing\RegionBuilderFake;
use Katalam\OnOfficeAdapter\Query\Testing\UserBuilderFake;

Expand All @@ -24,4 +25,12 @@ public function regions(): RegionBuilderFake
{
return new RegionBuilderFake($this->fakeResponses);
}

/**
* Returns a new fake imprint builder instance.
*/
public function imprint(): ImprintBuilderFake
{
return new ImprintBuilderFake($this->fakeResponses);
}
}
95 changes: 95 additions & 0 deletions src/Query/ImprintBuilder.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\Query\Concerns\NonFilterable;
use Katalam\OnOfficeAdapter\Query\Concerns\NonOrderable;
use Katalam\OnOfficeAdapter\Services\OnOfficeService;

class ImprintBuilder extends Builder
{
use NonFilterable;
use NonFilterable;
use NonOrderable;

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

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

return $this->onOfficeService->requestAll(/**
* @throws OnOfficeException
*/ function () use ($columns) {
return $this->onOfficeService->requestApi(
OnOfficeAction::Read,
OnOfficeResourceType::Impressum,
parameters: [
OnOfficeService::DATA => $columns,
]
);
}, pageSize: $listLimit, offset: $listOffset);
}

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

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

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::Read,
OnOfficeResourceType::Impressum,
resourceId: $id,
parameters: [
OnOfficeService::DATA => $columns,
]
);

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

/**
* @throws OnOfficeException
*/
public function each(callable $callback): void
{
throw new OnOfficeException('Not implemented');
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
7 changes: 7 additions & 0 deletions src/Query/Testing/ImprintBuilderFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Katalam\OnOfficeAdapter\Query\Testing;

class ImprintBuilderFake extends BaseFake
{
}
9 changes: 9 additions & 0 deletions src/Repositories/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Katalam\OnOfficeAdapter\Repositories;

use Katalam\OnOfficeAdapter\Query\ImprintBuilder;
use Katalam\OnOfficeAdapter\Query\RegionBuilder;
use Katalam\OnOfficeAdapter\Query\UserBuilder;
use Katalam\OnOfficeAdapter\Services\OnOfficeService;
Expand All @@ -28,4 +29,12 @@ public function regions(): RegionBuilder
{
return new RegionBuilder($this->onOfficeService);
}

/*
* Returns a new imprint builder instance.
*/
public function imprint(): ImprintBuilder
{
return new ImprintBuilder($this->onOfficeService);
}
}
20 changes: 20 additions & 0 deletions tests/Query/ImprintBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Support\Facades\Http;
use Katalam\OnOfficeAdapter\Facades\SettingRepository;
use Katalam\OnOfficeAdapter\Tests\Stubs\GetImprintResponse;

it('works', function () {
Http::preventStrayRequests();
Http::fake([
'*' => GetImprintResponse::make(),
]);

$imprint = SettingRepository::imprint()
->get();

expect($imprint)
->toHaveCount(1)
->and(data_get($imprint, '0.elements.firstname'))
->toBe('Max');
});
58 changes: 58 additions & 0 deletions tests/Stubs/GetImprintResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Katalam\OnOfficeAdapter\Tests\Stubs;

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

class GetImprintResponse
{
public static function make(array $data = []): PromiseInterface
{
return Http::response(self::getBody($data));
}

private static function getBody(array $data): 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' => '21',
'resourcetype' => 'impressum',
'cacheable' => true,
'identifier' => '',
'data' => [
'meta' => [
'cntabsolute' => null,
],
'records' => [
[
'id' => 'impressum',
'type' => null,
'elements' => [
'title' => 'Mr.',
'firstname' => 'Max',
'lastname' => 'Mustermann',
'firma' => 'onOffice GmbH',
'country' => 'Germany',
],
],
],
],
'status' => [
'errorcode' => 0,
'message' => 'OK',
],
],
],
],
], $data);
}
}

0 comments on commit 9d17f99

Please sign in to comment.