diff --git a/src/Handlers/BaseHandler.php b/src/Handlers/BaseHandler.php index e2bd393..cf2a22c 100644 --- a/src/Handlers/BaseHandler.php +++ b/src/Handlers/BaseHandler.php @@ -22,12 +22,13 @@ abstract public function get(string $class, string $property, ?string $context = * If the Handler supports saving values, it * MUST override this method to provide that functionality. * Not all Handlers will support writing values. + * Must throw RuntimeException for any failures. * * @param mixed $value * * @throws RuntimeException * - * @return mixed + * @return void */ public function set(string $class, string $property, $value = null, ?string $context = null) { @@ -38,10 +39,11 @@ public function set(string $class, string $property, $value = null, ?string $con * If the Handler supports forgetting values, it * MUST override this method to provide that functionality. * Not all Handlers will support writing values. + * Must throw RuntimeException for any failures. * * @throws RuntimeException * - * @return mixed + * @return void */ public function forget(string $class, string $property, ?string $context = null) { diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 3351efe..ef136d3 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -63,7 +63,7 @@ public function get(string $class, string $property, ?string $context = null) * * @throws RuntimeException For database failures * - * @return mixed|void + * @return void */ public function set(string $class, string $property, $value = null, ?string $context = null) { @@ -103,13 +103,13 @@ public function set(string $class, string $property, $value = null, ?string $con // Update storage $this->setStored($class, $property, $value, $context); - - return $result; } /** * Deletes the record from persistent storage, if found, * and from the local cache. + * + * @return void */ public function forget(string $class, string $property, ?string $context = null) { @@ -123,13 +123,11 @@ public function forget(string $class, string $property, ?string $context = null) ->delete(); if (! $result) { - return $result; + throw new RuntimeException(db_connect()->error()['message'] ?? 'Error writing to the database.'); } // Delete from local storage $this->forgetStored($class, $property, $context); - - return $result; } /** @@ -139,7 +137,7 @@ public function forget(string $class, string $property, ?string $context = null) * * @throws RuntimeException For database failures */ - private function hydrate(?string $context) + private function hydrate(?string $context): void { // Check for completion if (in_array($context, $this->hydrated, true)) { diff --git a/src/Settings.php b/src/Settings.php index 721fd93..1fe1237 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -3,6 +3,7 @@ namespace CodeIgniter\Settings; use CodeIgniter\Settings\Config\Settings as SettingsConfig; +use CodeIgniter\Settings\Handlers\BaseHandler; use InvalidArgumentException; use RuntimeException; @@ -14,19 +15,18 @@ class Settings { /** - * An array of Setting Stores that handle - * the actual act of getting/setting the values. + * An array of handlers for getting/setting the values. * - * @var array + * @var BaseHandler[] */ private $handlers = []; /** - * The name of the handler that handles writes. + * An array of the config options for each handler. * - * @var string + * @var array> */ - private $writeHandler; + private $options; /** * Grabs instances of our handlers. @@ -41,12 +41,7 @@ public function __construct(SettingsConfig $config) } $this->handlers[$handler] = new $class(); - - $writeable = $config->{$handler}['writeable'] ?? null; - - if ($writeable) { - $this->writeHandler = $handler; - } + $this->options[$handler] = $config->{$handler}; } } @@ -79,25 +74,31 @@ public function get(string $key, ?string $context = null) * * @param mixed $value * - * @return void|null + * @return void */ public function set(string $key, $value = null, ?string $context = null) { [$class, $property] = $this->prepareClassAndProperty($key); - return $this->getWriteHandler()->set($class, $property, $value, $context); + foreach ($this->getWriteHandlers() as $handler) { + $handler->set($class, $property, $value, $context); + } } /** * Removes a setting from the persistent storage, * effectively returning the value to the default value * found in the config file, if any. + * + * @return void */ public function forget(string $key, ?string $context = null) { [$class, $property] = $this->prepareClassAndProperty($key); - return $this->getWriteHandler()->forget($class, $property, $context); + foreach ($this->getWriteHandlers() as $handler) { + $handler->forget($class, $property, $context); + } } /** @@ -105,15 +106,23 @@ public function forget(string $key, ?string $context = null) * * @throws RuntimeException * - * @return mixed + * @return BaseHandler[] */ - private function getWriteHandler() + private function getWriteHandlers() { - if (empty($this->writeHandler) || ! isset($this->handlers[$this->writeHandler])) { + $handlers = []; + + foreach ($this->options as $handler => $options) { + if (! empty($options['writeable'])) { + $handlers[] = $this->handlers[$handler]; + } + } + + if ($handlers === []) { throw new RuntimeException('Unable to find a Settings handler that can store values.'); } - return $this->handlers[$this->writeHandler]; + return $handlers; } /** diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index e322735..fb603eb 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -38,9 +38,8 @@ protected function setUp(): void public function testSetInsertsNewRows() { - $results = $this->settings->set('Test.siteName', 'Foo'); + $this->settings->set('Test.siteName', 'Foo'); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -51,9 +50,8 @@ public function testSetInsertsNewRows() public function testSetInsertsBoolTrue() { - $results = $this->settings->set('Test.siteName', true); + $this->settings->set('Test.siteName', true); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -66,9 +64,8 @@ public function testSetInsertsBoolTrue() public function testSetInsertsBoolFalse() { - $results = $this->settings->set('Test.siteName', false); + $this->settings->set('Test.siteName', false); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -81,9 +78,8 @@ public function testSetInsertsBoolFalse() public function testSetInsertsNull() { - $results = $this->settings->set('Test.siteName', null); + $this->settings->set('Test.siteName', null); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -96,10 +92,9 @@ public function testSetInsertsNull() public function testSetInsertsArray() { - $data = ['foo' => 'bar']; - $results = $this->settings->set('Test.siteName', $data); + $data = ['foo' => 'bar']; + $this->settings->set('Test.siteName', $data); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -112,10 +107,9 @@ public function testSetInsertsArray() public function testSetInsertsObject() { - $data = (object) ['foo' => 'bar']; - $results = $this->settings->set('Test.siteName', $data); + $data = (object) ['foo' => 'bar']; + $this->settings->set('Test.siteName', $data); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -136,9 +130,8 @@ public function testSetUpdatesExistingRows() 'updated_at' => Time::now()->toDateTimeString(), ]); - $results = $this->settings->set('Test.siteName', 'Bar'); + $this->settings->set('Test.siteName', 'Bar'); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -148,9 +141,8 @@ public function testSetUpdatesExistingRows() public function testWorksWithoutConfigClass() { - $results = $this->settings->set('Nada.siteName', 'Bar'); + $this->settings->set('Nada.siteName', 'Bar'); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Nada', 'key' => 'siteName', @@ -170,9 +162,8 @@ public function testForgetSuccess() 'updated_at' => Time::now()->toDateTimeString(), ]); - $results = $this->settings->forget('Test.siteName'); + $this->settings->forget('Test.siteName'); - $this->assertTrue($results); $this->dontSeeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName', @@ -181,16 +172,18 @@ public function testForgetSuccess() public function testForgetWithNoStoredRecord() { - $results = $this->settings->forget('Test.siteName'); + $this->settings->forget('Test.siteName'); - $this->assertTrue($results); + $this->dontSeeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Test', + 'key' => 'siteName', + ]); } public function testSetWithContext() { - $results = $this->settings->set('Test.siteName', 'Banana', 'environment:test'); + $this->settings->set('Test.siteName', 'Banana', 'environment:test'); - $this->assertTrue($results); $this->seeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', 'key' => 'siteName',