Skip to content

Commit

Permalink
add Storage::clearValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed May 26, 2023
1 parent 8ea7fda commit 08a9459
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 54 deletions.
8 changes: 8 additions & 0 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ abstract public function get(): array;
* @return bool success.
*/
abstract public function clear(): bool;

/**
* Clear saved value for the specified item.
*
* @param string|int $key the key of the item to be cleared.
* @return bool success.
*/
abstract public function clearValue($key): bool;
}
10 changes: 10 additions & 0 deletions src/StorageArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public function clear(): bool

return true;
}

/**
* {@inheritDoc}
*/
public function clearValue($key): bool
{
unset($this->data[$key]);

return true;
}
}
75 changes: 58 additions & 17 deletions src/StorageDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace yii1tech\config;

use CDbCriteria;
use Yii;

/**
Expand All @@ -24,7 +25,8 @@
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class StorageDb extends Storage {
class StorageDb extends Storage
{
/**
* @var string id of the database connection application component.
*/
Expand All @@ -33,6 +35,14 @@ class StorageDb extends Storage {
* @var string name of the table, which should store values.
*/
public $table = 'app_config';
/**
* @var string name of the column, which should store config item key.
*/
public $keyColumn = 'id';
/**
* @var string name of the column, which should store config item value.
*/
public $valueColumn = 'value';

/**
* @return \CDbConnection database connection application component.
Expand All @@ -47,22 +57,36 @@ public function getDbConnection()
*/
public function save(array $values): bool
{
$this->clear();

$data = [];
foreach ($values as $id => $value) {
$data[] = array(
'id' => $id,
'value' => $value,
);
}
$existingValues = $this->get();

$insertedRowsCount = $this->getDbConnection()
->getCommandBuilder()
->createMultipleInsertCommand($this->table, $data)
->execute();
foreach ($values as $key => $value) {
if (array_key_exists($key, $existingValues)) {
if ($value === $existingValues[$key]) {
continue;
}

$criteria = new CDbCriteria();
$criteria->addColumnCondition([$this->keyColumn => $key]);
$data = [$this->valueColumn => $value];

$this->getDbConnection()
->getCommandBuilder()
->createUpdateCommand($this->table, $data, $criteria)
->execute();
} else {
$data = [
$this->keyColumn => $key,
$this->valueColumn => $value,
];

$this->getDbConnection()
->getCommandBuilder()
->createInsertCommand($this->table, $data)
->execute();
}
}

return (count($values) == $insertedRowsCount);
return true;
}

/**
Expand All @@ -76,7 +100,7 @@ public function get(): array

$values = [];
foreach ($rows as $row) {
$values[$row['id']] = $row['value'];
$values[$row[$this->keyColumn]] = $row[$this->valueColumn];
}

return $values;
Expand All @@ -87,7 +111,24 @@ public function get(): array
*/
public function clear(): bool
{
$this->getDbConnection()->createCommand()->delete($this->table);
$this->getDbConnection()
->createCommand()
->delete($this->table);

return true;
}

/**
* {@inheritDoc}
*/
public function clearValue($key): bool
{
$criteria = new CDbCriteria();
$criteria->addColumnCondition([$this->keyColumn => $key]);

$this->getDbConnection()
->createCommand()
->delete($this->table, $criteria->condition, $criteria->params);

return true;
}
Expand Down
52 changes: 41 additions & 11 deletions src/StoragePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class StoragePhp extends Storage {
class StoragePhp extends Storage
{
/**
* @var string name of the file, which should be used to store values.
*/
Expand Down Expand Up @@ -55,17 +56,9 @@ protected function defaultFileName(): string
*/
public function save(array $values): bool
{
$this->clear();
$values = array_merge($this->get(), $values);

$fileName = $this->getFileName();

$dirName = dirname($fileName);
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}

$bytesWritten = file_put_contents($fileName, $this->composeFileContent($values));
$this->invalidateScriptCache($fileName);
$bytesWritten = $this->saveToFile($values);

return ($bytesWritten > 0);
}
Expand Down Expand Up @@ -98,6 +91,43 @@ public function clear(): bool
return true;
}

/**
* {@inheritdoc}
*/
public function clearValue($key): bool
{
$values = $this->get();
if (empty($values)) {
return true;
}

unset($values[$key]);

$bytesWritten = $this->saveToFile($values);

return ($bytesWritten > 0);
}

/**
* Saves given values to the file.
* @param array $values values to be saved.
* @return int number of bytes written.
*/
protected function saveToFile(array $values)
{
$fileName = $this->getFileName();

$dirName = dirname($fileName);
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}

$bytesWritten = file_put_contents($fileName, $this->composeFileContent($values));
$this->invalidateScriptCache($fileName);

return $bytesWritten;
}

/**
* Composes file content for the given values.
* @param array $values values to be saved.
Expand Down
7 changes: 3 additions & 4 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Yii;
use yii1tech\config\Item;
use yii1tech\config\Manager;
use yii1tech\config\StoragePhp;
use yii1tech\config\StorageArray;

class ManagerTest extends TestCase
{
Expand Down Expand Up @@ -52,8 +52,7 @@ protected function createTestManager(): Manager
$config = [
'class' => Manager::class,
'storage' => [
'class' => StoragePhp::class,
'fileName' => $this->getTestFileName(),
'class' => StorageArray::class,
],
];

Expand All @@ -73,7 +72,7 @@ public function testSetGet(): void
$manager->setItems($items);
$this->assertEquals($items, $manager->getItems(), 'Unable to setup items!');

$storage = new StoragePhp();
$storage = new StorageArray();
$manager->setStorage($storage);
$this->assertEquals($storage, $manager->getStorage(), 'Unable to setup storage!');
}
Expand Down
19 changes: 19 additions & 0 deletions tests/StorageArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,23 @@ public function testClear()

$this->assertEmpty($this->storage->get());
}

/**
* @depends testSave
*/
public function testClearValue()
{
$values = [
'test.name' => 'Test name',
'test.title' => 'Test title',
];

$this->storage->save($values);
$this->storage->clearValue('test.name');

$returnedValues = $this->storage->get();

$this->assertFalse(array_key_exists('test.name', $returnedValues));
$this->assertTrue(array_key_exists('test.title', $returnedValues));
}
}
39 changes: 25 additions & 14 deletions tests/StorageDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ protected function setUp(): void
{
parent::setUp();

$this->createTestConfigTable();
$this->createTestDbSchema();
}

/**
* @return string test table name
*/
protected function getTestTableName(): string
{
return '_test_config';
return 'test_config';
}

/**
* Creates test config table.
*/
protected function createTestConfigTable(): void
protected function createTestDbSchema(): void
{
$dbConnection = Yii::app()->db;
$columns = [
Expand All @@ -41,12 +41,13 @@ protected function createTestConfigTable(): void
/**
* @return \yii1tech\config\StorageDb test storage instance.
*/
protected function createTestStorage() {
$config = array(
protected function createTestStorage(): StorageDb
{
$config = [
'class' => StorageDb::class,
'db' => 'db',
'table' => $this->getTestTableName(),
);
];

return Yii::createComponent($config);
}
Expand All @@ -61,35 +62,45 @@ public function testSave(): void
'name2' => 'value2',
];
$this->assertTrue($storage->save($values), 'Unable to save values!');

$returnedValues = $storage->get();

$this->assertEquals($values, $returnedValues, 'Unable to get values!');
}

/**
* @depends testSave
*/
public function testGet(): void
public function testClear(): void
{
$storage = $this->createTestStorage();
$values = [
'name1' => 'value1',
'name2' => 'value2',
];
$storage->save($values);
$this->assertEquals($values, $storage->get(), 'Unable to get values!');

$this->assertTrue($storage->clear(), 'Unable to clear values!');
$this->assertEmpty($storage->get(), 'Values are not cleared!');
}

/**
* @depends testGet
* @depends testSave
*/
public function testClear(): void
public function testClearValue()
{
$storage = $this->createTestStorage();
$values = [
'name1' => 'value1',
'name2' => 'value2',
'test.name' => 'Test name',
'test.title' => 'Test title',
];

$storage->save($values);
$storage->clearValue('test.name');

$this->assertTrue($storage->clear(), 'Unable to clear values!');
$this->assertEquals([], $storage->get(), 'Values are not cleared!');
$returnedValues = $storage->get();

$this->assertFalse(array_key_exists('test.name', $returnedValues));
$this->assertTrue(array_key_exists('test.title', $returnedValues));
}
}
Loading

0 comments on commit 08a9459

Please sign in to comment.