mirror of
https://github.com/friendica/friendica
synced 2025-04-21 01:50:17 +00:00
Make Storage testable & add tests
- Making StorageManager dynamic (DI::facStorage()) - Making concrete Storage dynamic (DI::storage()) - Add tests for Storage backend and failure handling - Bumping Level-2/Dice to "dev-master" until new release - Using Storage-Names instead of Storage-Classes in config (includes migration)
This commit is contained in:
parent
a5895f8623
commit
08edeae2f9
18 changed files with 744 additions and 242 deletions
|
@ -6,9 +6,8 @@
|
|||
|
||||
namespace Friendica\Model\Storage;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Database\DBA;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* @brief Database based storage system
|
||||
|
@ -17,47 +16,93 @@ use Friendica\Database\DBA;
|
|||
*/
|
||||
class Database implements IStorage
|
||||
{
|
||||
public static function get($ref)
|
||||
const NAME = 'Database';
|
||||
|
||||
/** @var \Friendica\Database\Database */
|
||||
private $dba;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
/** @var L10n\L10n */
|
||||
private $l10n;
|
||||
|
||||
/**
|
||||
* @param \Friendica\Database\Database $dba
|
||||
* @param LoggerInterface $logger
|
||||
* @param L10n\L10n $l10n
|
||||
*/
|
||||
public function __construct(\Friendica\Database\Database $dba, LoggerInterface $logger, L10n\L10n $l10n)
|
||||
{
|
||||
$r = DBA::selectFirst('storage', ['data'], ['id' => $ref]);
|
||||
if (!DBA::isResult($r)) {
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $reference)
|
||||
{
|
||||
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
|
||||
if (!$this->dba->isResult($result)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $r['data'];
|
||||
return $result['data'];
|
||||
}
|
||||
|
||||
public static function put($data, $ref = '')
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function put(string $data, string $reference = '')
|
||||
{
|
||||
if ($ref !== '') {
|
||||
$r = DBA::update('storage', ['data' => $data], ['id' => $ref]);
|
||||
if ($r === false) {
|
||||
Logger::log('Failed to update data with id ' . $ref . ': ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
|
||||
throw new StorageException(L10n::t('Database storage failed to update %s', $ref));
|
||||
if ($reference !== '') {
|
||||
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
|
||||
if ($result === false) {
|
||||
$this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
||||
throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
|
||||
}
|
||||
return $ref;
|
||||
|
||||
return $reference;
|
||||
} else {
|
||||
$r = DBA::insert('storage', ['data' => $data]);
|
||||
if ($r === false) {
|
||||
Logger::log('Failed to insert data: ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
|
||||
throw new StorageException(L10n::t('Database storage failed to insert data'));
|
||||
$result = $this->dba->insert('storage', ['data' => $data]);
|
||||
if ($result === false) {
|
||||
$this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
|
||||
throw new StorageException($this->l10n->t('Database storage failed to insert data'));
|
||||
}
|
||||
return DBA::lastInsertId();
|
||||
|
||||
return $this->dba->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
||||
public static function delete($ref)
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function delete(string $reference)
|
||||
{
|
||||
return DBA::delete('storage', ['id' => $ref]);
|
||||
return $this->dba->delete('storage', ['id' => $reference]);
|
||||
}
|
||||
|
||||
public static function getOptions()
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function saveOptions($data)
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function saveOptions(array $data)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue