Skip to content

Commit

Permalink
Merge branch 'feature/typo3v12update' into 'main'
Browse files Browse the repository at this point in the history
Feature/typo3v12update

See merge request fairway/fairway-pixelbox-saas-api!1
  • Loading branch information
ferfrost committed Sep 26, 2023
2 parents 7f0d624 + ef42ed1 commit 2b2f3c0
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"php": "^7.4 || ^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3 || ^7.3",
"psr/log": "^1.1 || ^3.0"
"psr/log": "^1.1 || ^3.0",
"fairway/filesystem-api": "@dev"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
61 changes: 61 additions & 0 deletions src/Adapter/Directory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the "fairway_api" library by eCentral GmbH.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Fairway\PixelboxxSaasApi\Adapter;

use Fairway\PixelboxxSaasApi\Model\Folder;

final class Directory extends \Fairway\FairwayFilesystemApi\Directory
{
private Folder $folder;
private ?Directory $parent;

public function __construct(Folder $folder, Directory $parent = null)
{
$this->folder = $folder;
$this->parent = $parent;
}

public function getIdentifier(): string
{
if ($this->folder->getId()->getResourceId() === '0') {
return '';
}
$parent = '';
if ($this->parent) {
$parent = $this->parent->getIdentifier();
}
return $parent . '/' . $this->folder->getId()->getResourceId();
}

public function getFileName(): string
{
return $this->folder->getName();
}

public function getATime(): int
{
return 0;
}

public function getMTime(): int
{
return 0;
}

public function getCTime(): int
{
return 0;
}

public function getParentDirectory(): self
{
return $this->parent;
}
}
236 changes: 236 additions & 0 deletions src/Adapter/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "fairway_api" library by eCentral GmbH.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Fairway\PixelboxxSaasApi\Adapter;

use Exception;
use Fairway\FairwayFilesystemApi\DirectoryIterator;
use Fairway\FairwayFilesystemApi\DriverClient;
use Fairway\FairwayFilesystemApi\Exceptions\NotImplementedException;
use Fairway\FairwayFilesystemApi\Exceptions\NotSupportedException;
use Fairway\FairwayFilesystemApi\File;
use Fairway\FairwayFilesystemApi\FileType;
use Fairway\FairwayFilesystemApi\Permission;
use Fairway\PixelboxxSaasApi\Client;
use Fairway\PixelboxxSaasApi\Model\Folder;
use Fairway\PixelboxxSaasApi\Model\MetadataGroup;
use Fairway\PixelboxxSaasApi\PixelboxxResourceName;
use Fairway\PixelboxxSaasApi\Request\Assets\AssetMetadata;
use Fairway\PixelboxxSaasApi\Utility\PixelboxxUtility;

final class Driver implements DriverClient
{
private Client $client;

public function __construct(Client $client)
{
$this->client = $client;
}

public function getClient(): Client
{
return $this->client;
}

public function hasAssetPicker(): bool
{
return true;
}

public function getAssetPicker(): string
{
throw new NotImplementedException();
}

public function getFile(string $identifier): File
{
return new PixelboxxFile($this, $identifier);
}

public function getPublicUrl(string $identifier): string
{
return $this->client->assets()->getThumbnail($identifier)->getUrl();
}

public function getMetadata(string $identifier): array
{
if ($this->getType($identifier) === FileType::DIRECTORY) {
throw new NotSupportedException();
}
/** @var MetadataGroup[] $metadataGroups */
$metadataGroups = $this->client->assets()->getAsset($identifier, function (AssetMetadata $metadata) {
$metadata->setFull(true);
$metadata->setLocalizedValues(true);
$metadata->setAllMeta(true);
})->getAsset()->getMetadata();
$metadata = [];
foreach ($metadataGroups as $metadataGroup) {
foreach ($metadataGroup->getProperties() as $property) {
// todo: There should be a metadata model
$metadata[] = $property;
}
}
return $metadata;
}

public function exists(string $identifier, string $type): bool
{
if ($type === FileType::FILE) {
return $this->client->assets()->getAsset($identifier) !== null;
}
return $this->client->folders()->getFolderAssets($identifier) !== null;
}

public function getType(string $identifier): string
{
try {
$identifier = new PixelboxxResourceName($identifier);
} catch (Exception $exception) {
throw new NotSupportedException('Identifier is not supported');
}
if ($identifier->getResourceType() === PixelboxxResourceName::FOLDER) {
return FileType::DIRECTORY;
}
if ($identifier->getResourceType() === PixelboxxResourceName::ASSET) {
return FileType::FILE;
}
throw new NotSupportedException(sprintf('File type %s is not supported', $identifier->getResourceType()));
}

public function read(string $identifier): string
{
return $this->client->assets()->downloadAsset($identifier)->getContent();
}

public function listDirectory(string $identifier = null, bool $recursive = false): DirectoryIterator
{
$structure = $this->client->folders()->getFolderStructure($identifier);
$directories = [];
if ($structure !== null) {
$this->flattenDirectoryStructure($structure->getFolder(), $directories, null, $recursive);
}
return new DirectoryIterator($directories);
}

public function getDirectory(string $identifier = null): ?Directory
{
$folder = $this->client->folders()->getFolderStructure($identifier);
if ($folder === null) {
return null;
}
return new Directory($folder->getFolder());
}

private function flattenDirectoryStructure(Folder $folder, array &$directories, Directory $parent = null, bool $recursive = false): void
{
$parentDirectory = new Directory($folder, $parent);
foreach ($folder->getChildFolders() as $childFolder) {
$directories[] = new Directory($childFolder, $parentDirectory);
if ($recursive) {
$this->flattenDirectoryStructure($childFolder, $directories, $parentDirectory, $recursive);
}
}
}

public function lastModified(string $identifier): int
{
// @todo Put field names into constants?
$metadata = $this->client->assets()->getAsset($identifier)->getAsset()->getMetadataByPropertyId('versiondate');
return PixelboxxUtility::buildTimestamp($metadata->getValue());
}

public function size(string $identifier): int
{
$metadata = $this->client->assets()->getAsset($identifier)->getAsset()->getMetadataByPropertyId('filesize');
return (int)$metadata->getValue();
}

public function count(string $identifier): int
{
if ($this->getType($identifier) === FileType::FILE) {
throw new NotSupportedException('The type File cannot be counted');
}
$folder = $this->client->folders()->getFolderStructure($identifier);
return $folder === null ? 0 : $folder->getCount();
}

public function mimeType(string $identifier): string
{
$metadata = $this->client->assets()->getAsset($identifier)->getAsset()->getMetadataByPropertyId('type');
return $metadata->getValue();
}

public function visibility(string $identifier): string
{
// todo: this needs to be refined, how are we going to implement visibility
return '';
}

public function getPermission(string $identifier): Permission
{
return new PixelboxxPermission($identifier, true, true);
}

public function write(string $identifier, string $parentIdentifier, string $filePath, array $config = []): string
{
throw new NotImplementedException();
}

public function setVisibility(string $identifier): void
{
throw new NotImplementedException();
}

public function delete(string $identifier): void
{
throw new NotImplementedException();
}

public function create(string $identifier, string $parentIdentifier, array $config = []): string
{
throw new NotImplementedException();
}

public function move(string $identifier, string $oldDestination, string $destination, array $config = []): void
{
throw new NotImplementedException();
}

public function copy(string $identifier, string $destination, array $config = []): void
{
throw new NotImplementedException();
}

public function rename(string $identifier, string $newName, array $config = []): void
{
throw new NotImplementedException();
}

public function replace(string $identifier, string $filePath, array $config = []): string
{
throw new NotImplementedException();
}

public function parentOfIdentifier(string $identifier): DirectoryIterator
{
$file = $this->getClient()->assets()->getAsset($identifier)->getAsset();
$directories = [];
foreach ($file->getContainingFolders() as $parentFolder) {
$directories[] = $this->getDirectory((string)$parentFolder);
}
return new DirectoryIterator($directories);
}

public function getDriver(): DriverClient
{
return $this;
}
}
59 changes: 59 additions & 0 deletions src/Adapter/PixelboxxClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "fairway_api" library by eCentral GmbH.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Fairway\PixelboxxSaasApi\Adapter;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

final class PixelboxxClient
{
private ClientInterface $client;
private array $defaultConfiguration = [];
private array $configuration = [];
private string $baseUrl;

public function __construct(string $baseUrl, array $configuration = [])
{
$this->baseUrl = $baseUrl;
$this->configuration = $configuration;
}

public function authenticate(string $username, string $password)
{
$client = new Client($this->configuration);
$response = $client->post(
$this->getEndpoint('/authenticate/login'),
[
'auth' => [
$username,
$password
]
]
);
}

private function getConfiguration(): array
{
return array_merge($this->defaultConfiguration, $this->configuration);
}

public function setConfiguration(array $configuration): self
{
$this->configuration = $configuration;
return $this;
}

public function getEndpoint(string $endpoint): string
{
return sprintf('%s%s', rtrim($this->baseUrl, '/'), $endpoint);
}
}
Loading

0 comments on commit 2b2f3c0

Please sign in to comment.