Commit ef7194a0 authored by Björn Hjortsten's avatar Björn Hjortsten

Added support for images

Uploading, fetching, listing, updating and deletion.
parent b247e0f4
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi\Controller;
use GuzzleHttp\Exception\TransferException;
use QBNK\TemplafyApi\Model\Image;
use QBNK\TemplafyApi\Model\ImageResponse;
class ImageController extends BaseController {
/**
* @param string|null $folderId
* @param string|null $navigationPath
* @param string|null $searchQuery
* @param int|null $pageNumber
* @param int|null $pageSize
* @return ImageResponse[]
*/
public function list(string $folderId = null, string $navigationPath = null, string $searchQuery = null, int $pageNumber = null, int $pageSize = null): array {
$queryParameters = [];
if ($folderId !== null) {
$queryParameters['folderId'] = $folderId;
}
if ($navigationPath !== null) {
$queryParameters['navigationPath'] = $navigationPath;
}
if ($searchQuery !== null) {
$queryParameters['searchQuery'] = $searchQuery;
}
if ($pageNumber !== null) {
$queryParameters['pageNumber'] = $pageNumber;
}
if ($pageSize !== null) {
$queryParameters['pageSize'] = $pageSize;
}
$response = $this->apiHttpClient->get('/images', ['query' => $queryParameters]);
$responseData = \GuzzleHttp\json_decode($response->getBody(), true);
$result = [];
foreach ($responseData as $rawFolder) {
$result[] = ImageResponse::fromArray($rawFolder);
}
return $result;
}
public function create(Image $image, string $pathToFile): ImageResponse {
$response = $this->apiHttpClient->post('/images', [
'multipart' => [
['name' => 'name', 'contents' => $image->getName()],
['name' => 'folderId', 'contents' => $image->getFolderId()],
['name' => 'tags', 'contents' => $image->getTags()],
['name' => 'externalData', 'contents' => $image->getExternalData()],
['name' => 'file', 'contents' => fopen($pathToFile, 'r')]
]
]);
if ($response->getStatusCode() !== 201) {
throw new TransferException('Non-successful response to upload: '.$response->getStatusCode().' '.$response->getReasonPhrase());
}
$assetUrl = $response->getHeader('Location')[0];
$imageId = str_replace('https://api-v1.templafy.com/images/', '', $assetUrl);
sleep(1); // Do not fetch to early as we get a 404
return $this->get($imageId);
}
public function get(string $id): ImageResponse {
$response = $this->apiHttpClient->get('/images/'.$id);
$responseData = \GuzzleHttp\json_decode($response->getBody(), true);
return ImageResponse::fromArray($responseData);
}
public function update(string $id, Image $image, string $pathToFile): ImageResponse {
$response = $this->apiHttpClient->patch('/images/'.$id, [
'multipart' => [
['name' => 'name', 'contents' => $image->getName()],
['name' => 'folderId', 'contents' => $image->getFolderId()],
['name' => 'tags', 'contents' => $image->getTags()],
['name' => 'externalData', 'contents' => $image->getExternalData()],
['name' => 'file', 'contents' => fopen($pathToFile, 'r')]
]
]);
return $this->get($id);
}
public function delete(string $id): void {
$this->apiHttpClient->delete('/images/'.$id);
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi\Model;
use JsonSerializable;
class Image implements JsonSerializable {
/** @var string */
protected $name;
/** @var string */
protected $folderId;
/** @var string */
protected $tags;
/** @var string */
protected $externalData;
/**
* @param string $name
* @return Image
*/
public function setName(string $name): Image {
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getName(): string {
return $this->name;
}
/**
* @param string $folderId
* @return Image
*/
public function setFolderId(string $folderId): Image {
$this->folderId = $folderId;
return $this;
}
/**
* @return string
*/
public function getFolderId(): string {
return $this->folderId;
}
/**
* @param string $tags
* @return Image
*/
public function setTags(string $tags): Image {
$this->tags = $tags;
return $this;
}
/**
* @return string
*/
public function getTags(): string {
return $this->tags ?? '';
}
/**
* @param string $externalData
* @return Image
*/
public function setExternalData(string $externalData): Image {
$this->externalData = $externalData;
return $this;
}
/**
* @return string
*/
public function getExternalData(): string {
return $this->externalData ?? '';
}
/**
* @return Image
*/
public static function fromArray(array $data) {
$instance = new static();
$instance
->setName((string)$data['name'])
->setTags((string)$data['tags'])
->setFolderId((string)$data['folderId'])
->setExternalData((string)$data['externalData'])
;
return $instance;
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'name' => $this->getName(),
'folderId' => $this->getFolderId(),
'tags' => $this->getTags(),
'externalData' => $this->getExternalData()
];
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi\Model;
class ImageResponse extends Image {
/** @var string */
protected $id;
/** @var string */
protected $mimeType;
/** @var int */
protected $width;
/** @var int */
protected $height;
/** @var int */
protected $fileSize;
/** @var string */
protected $checksum;
/** @var string */
protected $previewUrl;
/** @var string */
protected $downloadUrl;
/**
* @return string
*/
public function getId(): string {
return $this->id;
}
/**
* @return string
*/
public function getMimeType(): string {
return $this->mimeType;
}
/**
* @return int
*/
public function getWidth(): int {
return $this->width;
}
/**
* @return int
*/
public function getHeight(): int {
return $this->height;
}
/**
* @return int
*/
public function getFileSize(): int {
return $this->fileSize;
}
/**
* @return string
*/
public function getChecksum(): string {
return $this->checksum;
}
/**
* @return string
*/
public function getPreviewUrl(): string {
return $this->previewUrl;
}
/**
* @return string
*/
public function getDownloadUrl(): string {
return $this->downloadUrl;
}
/**
* @param string $id
* @return ImageResponse
*/
protected function setId(string $id): ImageResponse {
$this->id = $id;
return $this;
}
/**
* @param string $mimeType
* @return ImageResponse
*/
protected function setMimeType(string $mimeType): ImageResponse {
$this->mimeType = $mimeType;
return $this;
}
/**
* @param int $width
* @return ImageResponse
*/
protected function setWidth(int $width): ImageResponse {
$this->width = $width;
return $this;
}
/**
* @param int $height
* @return ImageResponse
*/
protected function setHeight(int $height): ImageResponse {
$this->height = $height;
return $this;
}
/**
* @param int $fileSize
* @return ImageResponse
*/
protected function setFileSize(int $fileSize): ImageResponse {
$this->fileSize = $fileSize;
return $this;
}
/**
* @param string $checksum
* @return ImageResponse
*/
protected function setChecksum(string $checksum): ImageResponse {
$this->checksum = $checksum;
return $this;
}
/**
* @param string $previewUrl
* @return ImageResponse
*/
protected function setPreviewUrl(string $previewUrl): ImageResponse {
$this->previewUrl = $previewUrl;
return $this;
}
/**
* @param string $downloadUrl
* @return ImageResponse
*/
protected function setDownloadUrl(string $downloadUrl): ImageResponse {
$this->downloadUrl = $downloadUrl;
return $this;
}
/**
* @return ImageResponse
*/
public static function fromArray(array $data) {
$instance = parent::fromArray($data);
$instance
->setId($data['id'])
->setMimeType((string)$data['mimeType'])
->setWidth((int)$data['width'])
->setHeight((int)$data['height'])
->setFileSize((int)$data['fileSize'])
->setChecksum((string)$data['checksum'])
->setPreviewUrl((string)$data['previewUrl'])
->setDownloadUrl((string)$data['downloadUrl'])
;
return $instance;
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ namespace QBNK\TemplafyApi;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use QBNK\TemplafyApi\Controller\FolderController;
use QBNK\TemplafyApi\Controller\ImageController;
use Sainsburys\Guzzle\Oauth2\GrantType\ClientCredentials;
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshToken;
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware;
......@@ -28,6 +29,9 @@ class TemplafyApi {
/** @var FolderController */
protected $folderController;
/** @var ImageController */
protected $imageController;
public function __construct(Credentials $credentials, array $options = []) {
$this->credentials = $credentials;
}
......@@ -73,10 +77,17 @@ class TemplafyApi {
return $this->apiClient;
}
public function folders() {
public function folders(): FolderController {
if (!($this->folderController instanceof FolderController)) {
$this->folderController = new FolderController($this->getHttpClient());
}
return $this->folderController;
}
public function images(): ImageController {
if (!($this->imageController instanceof ImageController)) {
$this->imageController = new ImageController($this->getHttpClient());
}
return $this->imageController;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment