Skip to content

Commit

Permalink
Merge pull request #14 from czarpino/add-user-metadata-calls
Browse files Browse the repository at this point in the history
Add user metadata calls
  • Loading branch information
czarpino authored May 8, 2019
2 parents 8927ca9 + 1b0ae9b commit 8acf44b
Show file tree
Hide file tree
Showing 5 changed files with 737 additions and 1 deletion.
129 changes: 128 additions & 1 deletion fccloudapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,41 @@ protected function reverseCastFromType($data, int $type)

return $data;
}

/**
* Best effort guess of datatype
*
* @param $data
* @return int
*/
protected function guessType($data)
{
if ($data instanceof \DateTime) {
return MetadataAttributeTypes::TYPE_DATE;
}

if (is_int($data)) {
return MetadataAttributeTypes::TYPE_INTEGER;
}

if (is_float($data)) {
return MetadataAttributeTypes::TYPE_DECIMAL;
}

if (is_bool($data)) {
return MetadataAttributeTypes::TYPE_BOOLEAN;
}

if (is_array($data)) {
return MetadataAttributeTypes::TYPE_ARRAY;
}

return MetadataAttributeTypes::TYPE_TEXT;
}
}

/**
* Class AbstractMetadataRecord
* Class MetadataAttributeTypes
* @package codelathe\fccloudapi
*/
final class MetadataAttributeTypes
Expand Down Expand Up @@ -2733,6 +2764,8 @@ protected function doChunkedUpload($url, $filechunkpath, $filename) {
}

class CloudAPI extends APICore {

use MetadataAttributeTypeCasterTrait;

public function __construct($SERVER_URL, $debug = false) {
parent::__construct($SERVER_URL, $debug);
Expand Down Expand Up @@ -4619,6 +4652,100 @@ public function getMetadataValues(string $fullPath)

return $collection;
}

/**
* Get metadata sets for search
*
* @param string $fullPath
* @return Collection|null
*/
public function getMetadataSetsForSearch(string $fullPath): ?Collection
{
$this->startTimer();
$response = $this->doPOST("{$this->server_url}/core/getmetadatasetsforsearch", http_build_query([
'fullpath' => $fullPath
]));
$collection = new Collection($response, 'metadataset', MetadataSetRecord::class);
$this->stopTimer();

return $collection;
}

/**
* Add specified metadata set to file object
*
* @param string $fullPath
* @param string $setId
* @return CommandRecord|null
*/
public function addSetToFileObject(string $fullPath, string $setId): ?CommandRecord
{
$this->startTimer();
$response = $this->doPOST("{$this->server_url}/core/addsettofileobject", http_build_query([
'fullpath' => $fullPath,
'setid' => $setId,
]));
$collection = new Collection($response, 'command', CommandRecord::class);
$records = $collection->getRecords();
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
$this->stopTimer();

return $record;
}

/**
* Remove specified metadata set from file object
*
* @param string $fullPath
* @param string $setId
* @return CommandRecord|null
*/
public function removeSetFromFileObject(string $fullPath, string $setId): ?CommandRecord
{
$this->startTimer();
$response = $this->doPOST("{$this->server_url}/core/removesetfromfileobject", http_build_query([
'fullpath' => $fullPath,
'setid' => $setId,
]));
$collection = new Collection($response, 'command', CommandRecord::class);
$records = $collection->getRecords();
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
$this->stopTimer();

return $record;
}

/**
* Update attribute values of a file object's metadata
*
* @param string $setId
* @param string $fullPath
* @param array $attributes
* @return CommandRecord|null
*/
public function saveAttributeValues(string $fullPath, string $setId, array $attributes): ?CommandRecord
{
$this->startTimer();
$args = [
'fullpath' => $fullPath,
'setid' => $setId,
];

foreach ($attributes as $i => $attribute) {
$args["attribute{$i}_attributeid"] = $attribute['attributeid'];
$args["attribute{$i}_value"] = $this->reverseCastFromType($attribute['value'], $this->guessType($attribute['value']));
}

$args['attributes_total'] = count($attributes);

$response = $this->doPOST("{$this->server_url}/core/saveattributevalues", http_build_query($args));
$collection = new Collection($response, 'command', CommandRecord::class);
$records = $collection->getRecords();
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
$this->stopTimer();

return $record;
}

public function getUITranslations() {
$this->startTimer();
Expand Down
89 changes: 89 additions & 0 deletions tests/CloudApi/AddSetToFileObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace CodeLathe\FileCloudApi\Tests\CloudApi;

use codelathe\fccloudapi\CloudAPI;
use codelathe\fccloudapi\CommandRecord;
use CodeLathe\FileCloudApi\Tests\Fixtures\AccessibleCloudApi;
use PHPUnit\Framework\TestCase;

class AddSetToFileObjectTest extends TestCase
{
public function testOnSuccess()
{
$serverUrl = 'https://fcapi.example.com';
$cloudApiMock = $this->getMockBuilder(AccessibleCloudApi::class)
->setConstructorArgs([$serverUrl])
->setMethods(['init', '__destruct', 'doPost'])
->getMock();

$mockApiResponse = <<<RESPONSE
<commands>
<command>
<type>addsettofileobject</type>
<result>1</result>
<message>Metadata set (setId: 5ccafe12adccf621f80342e6) was successfully added to File Object (/tester/textfile1.txt)</message>
</command>
</commands>
RESPONSE;
$mockApiRequest = $this->getValidApiRequest();

$cloudApiMock->method('doPost')
->with("{$serverUrl}/core/addsettofileobject", http_build_query($mockApiRequest))
->willReturn($mockApiResponse);

/** @var CloudAPI $cloudApiMock */
/** @var CommandRecord $commandRecord */
$commandRecord = $cloudApiMock->addSetToFileObject(...array_values($mockApiRequest));
$this->assertEquals(1, $commandRecord->getResult());
$this->assertEquals('addsettofileobject', $commandRecord->getType());
$this->assertEquals(
"Metadata set (setId: {$mockApiRequest['setid']}) was successfully added to File Object ({$mockApiRequest['fullpath']})",
$commandRecord->getMessage()
);
}

public function testOnFailure()
{
$serverUrl = 'https://fcapi.example.com';
$cloudApiMock = $this->getMockBuilder(AccessibleCloudApi::class)
->setConstructorArgs([$serverUrl])
->setMethods(['init', '__destruct', 'doPost'])
->getMock();

$mockApiResponse = <<<RESPONSE
<commands>
<command>
<type>addsettofileobject</type>
<result>0</result>
<message>Failed to bind Set Definition (setId: 5ccafe12adccf621f80342e7) to File Object (/tester/textfile1.txt). Reason: Incorrect set id provided</message>
</command>
</commands>
RESPONSE;
$mockApiRequest = $this->getValidApiRequest();
$mockApiRequest['setid'] = '5ccafe12adccf621f80342e7'; // Pretend this is an invalid id


$cloudApiMock->method('doPost')
->with("{$serverUrl}/core/addsettofileobject", http_build_query($mockApiRequest))
->willReturn($mockApiResponse);

/** @var CloudAPI $cloudApiMock */
/** @var CommandRecord $commandRecord */
$commandRecord = $cloudApiMock->addSetToFileObject(...array_values($mockApiRequest));
$this->assertEquals(0, $commandRecord->getResult());
$this->assertEquals('addsettofileobject', $commandRecord->getType());
$this->assertEquals(
"Failed to bind Set Definition (setId: {$mockApiRequest['setid']}) to File Object ({$mockApiRequest['fullpath']}). Reason: Incorrect set id provided",
$commandRecord->getMessage()
);
}

private function getValidApiRequest()
{
return [
'fullpath' => '/tester/textfile1.txt',
'setid' => '5ccafe12adccf621f80342e6',
];
}
}
Loading

0 comments on commit 8acf44b

Please sign in to comment.