Skip to content

Commit

Permalink
feat: add upload and link file
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed May 31, 2024
1 parent 508fa90 commit 65d2adc
Show file tree
Hide file tree
Showing 13 changed files with 576 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ enum OnOfficeResourceType: string
case Users = 'users';
case UnlockProvider = 'unlockProvider';
case Regions = 'regions';
case UploadFile = 'uploadfile';
}
27 changes: 27 additions & 0 deletions src/Facades/FileRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades;

use Illuminate\Support\Facades\Facade;
use Katalam\OnOfficeAdapter\Facades\Testing\FileRepositoryFake;
use Katalam\OnOfficeAdapter\Query\UploadBuilder;

/**
* @see \Katalam\OnOfficeAdapter\Repositories\SettingRepository
*
* @method static UploadBuilder upload()
*/
class FileRepository extends Facade
{
public static function fake(array ...$fakeResponses): FileRepositoryFake
{
static::swap($fake = new FileRepositoryFake(...$fakeResponses));

return $fake;
}

protected static function getFacadeAccessor(): string
{
return \Katalam\OnOfficeAdapter\Repositories\FileRepository::class;
}
}
18 changes: 18 additions & 0 deletions src/Facades/Testing/FileRepositoryFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades\Testing;

use Katalam\OnOfficeAdapter\Query\Testing\UploadBuilderFake;

class FileRepositoryFake
{
use FakeResponses;

/**
* Returns a new fake upload builder instance.
*/
public function upload(): UploadBuilderFake
{
return new UploadBuilderFake($this->fakeResponses);
}
}
12 changes: 6 additions & 6 deletions src/Facades/Testing/RecordFactories/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ final public function __construct()
{
}

public function id(int $id): self
public function id(int $id): static
{
$this->id = $id;

return $this;
}

public function type(string $type): self
public function type(string $type): static
{
$this->type = $type;

return $this;
}

public function set(string $key, mixed $value): self
public function set(string $key, mixed $value): static
{
$this->elements[$key] = $value;

return $this;
}

public function data(array $data): self
public function data(array $data): static
{
$this->elements = array_merge($this->elements, $data);

return $this;
}

public static function make(): self
public static function make(): static
{
return new static();
}
Expand All @@ -64,7 +64,7 @@ public function toArray(): array
];
}

public function __call(string $name, array $arguments): self
public function __call(string $name, array $arguments): static
{
if (str_starts_with($name, 'set')) {
$key = lcfirst(substr($name, 3));
Expand Down
55 changes: 55 additions & 0 deletions src/Facades/Testing/RecordFactories/FileUploadFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories;

use Illuminate\Support\Str;

class FileUploadFactory extends BaseFactory
{
public function id(int $id): static
{
return $this;
}

public function type(string $type): static
{
return $this;
}

public function elements(): static
{
return $this;
}

public function tmpUploadId(string $tmpUploadId = ''): static
{
if ($tmpUploadId === '') {
$tmpUploadId = Str::uuid()->toString();
}

$this->elements['tmpUploadId'] = $tmpUploadId;

return $this;
}

public function success(bool $success): static
{
$this->elements['success'] = $success ? 'success' : 'error';

return $this;
}

public function ok(): static
{
$this->elements['success'] = 'success';

return $this;
}

public function error(): static
{
$this->elements['success'] = 'error';

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@

class MarketPlaceUnlockProviderFactory extends BaseFactory
{
public function id(int $id): self
public function id(int $id): static
{
return $this;
}

public function type(string $type): self
public function type(string $type): static
{
return $this;
}

public function elements(): self
public function elements(): static
{
return $this;
}

public function success(bool $success): self
public function success(bool $success): static
{
$this->elements['success'] = $success ? 'success' : 'error';

return $this;
}

public function ok(): self
public function ok(): static
{
$this->elements['success'] = 'success';

return $this;
}

public function error(): self
public function error(): static
{
$this->elements['success'] = 'error';

Expand Down
38 changes: 38 additions & 0 deletions src/Query/Testing/UploadBuilderFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Katalam\OnOfficeAdapter\Query\Testing;

use Throwable;

class UploadBuilderFake extends BaseFake
{
/**
* @throws Throwable
*/
public function save(string $fileContent): string
{
$record = $this->get()->first();

return data_get($record, 'elements.tmpUploadId');
}

/**
* @throws Throwable
*/
public function link(string $tmpUploadId, array $data = []): bool
{
$record = $this->get()->first();

return data_get($record, 'elements.success') === 'success';
}

/**
* @throws Throwable
*/
public function saveAndLink(string $fileContent, array $data = []): bool
{
$tmpUploadId = $this->save($fileContent);

return $this->link($tmpUploadId, $data);
}
}
115 changes: 115 additions & 0 deletions src/Query/UploadBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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\Query\Concerns\NonSelectable;
use Katalam\OnOfficeAdapter\Services\OnOfficeService;

class UploadBuilder extends Builder
{
use NonFilterable;
use NonFilterable;
use NonOrderable;
use NonSelectable;

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

/**
* @throws OnOfficeException
*/
public function get(): Collection
{
throw new OnOfficeException('Method not implemented');
}

/**
* @throws OnOfficeException
*/
public function first(): array
{
throw new OnOfficeException('Method not implemented');
}

/**
* @throws OnOfficeException
*/
public function find(int $id): array
{
throw new OnOfficeException('Method not implemented');
}

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

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}

/**
* File content as base64-encoded binary data.
* Returns the temporary upload id.
*
* @throws OnOfficeException
*/
public function save(string $fileContent): string
{
$response = $this->onOfficeService->requestApi(
OnOfficeAction::Do,
OnOfficeResourceType::UploadFile,
parameters: [
OnOfficeService::DATA => $fileContent,
]
);

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

/**
* @throws OnOfficeException
*/
public function link(string $tmpUploadId, array $data = []): self
{
$data = array_replace($data, [
'tmpUploadId' => $tmpUploadId,
]);

$this->onOfficeService->requestApi(
OnOfficeAction::Do,
OnOfficeResourceType::UploadFile,
parameters: $data,
);

return $this;
}

/**
* File content as base64-encoded binary data.
* Returns the temporary upload id.
*
* @throws OnOfficeException
*/
public function saveAndLink(string $fileContent, array $data = []): self
{
$tmpUploadId = $this->save($fileContent);

return $this->link($tmpUploadId, $data);
}
}
22 changes: 22 additions & 0 deletions src/Repositories/FileRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Katalam\OnOfficeAdapter\Repositories;

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

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

/**
* Returns a new upload builder instance.
*/
public function upload(): UploadBuilder
{
return new UploadBuilder($this->onOfficeService);
}
}
Loading

0 comments on commit 65d2adc

Please sign in to comment.