Skip to content

Commit

Permalink
PHP Linting (Pint)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk authored and github-actions[bot] committed Sep 27, 2023
1 parent ad3f4f4 commit ffcf3ec
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 95 deletions.
2 changes: 2 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [
/*
|--------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/Drivers/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function extend(string $driver, Closure $callback): self
return $this;
}

public function setDefaultDriver(string $driver): void
{
$this->app['config']['settings.driver'] = $driver;
}

protected function createDatabaseDriver(array $config): DatabaseDriver
{
return new DatabaseDriver(
Expand All @@ -54,11 +59,6 @@ protected function getDefaultDriver(): string
return $this->app['config']['settings.driver'];
}

public function setDefaultDriver(string $driver): void
{
$this->app['config']['settings.driver'] = $driver;
}

protected function getDriverConfig(string $driver): ?array
{
return $this->app['config']["settings.drivers.{$driver}"];
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

class Setting extends Model implements SettingContract
{
public $timestamps = false;

protected ?string $teamForeignKey = null;

protected $guarded = ['id'];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
Expand All @@ -20,10 +24,6 @@ public function __construct(array $attributes = [])
$this->teamForeignKey = config('settings.team_foreign_key');
}

protected $guarded = ['id'];

public $timestamps = false;

public static function getValue(string $key, $default = null, $teamId = null)
{
$value = static::query()
Expand Down
140 changes: 70 additions & 70 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,62 +213,6 @@ public function isTrue(string $key, $default = true): bool
return $value === true || $value === '1' || $value === 1;
}

protected function normalizeKey(string $key): string
{
if (Str::startsWith(haystack: $key, needles: 'file_')) {
return str_replace(search: 'file_', replace: 'file.', subject: $key);
}

return $key;
}

protected function getCacheKey(string $key): string
{
$cacheKey = $this->cacheKeyPrefix . $key;

if ($this->teams) {
$teamId = $this->teamId ?? 'null';

$cacheKey .= "::team:{$teamId}";
}

return $cacheKey;
}

protected function getKeyForStorage(string $key): string
{
return $this->keyGenerator->generate(key: $key, context: $this->context);
}

protected function serializeValue($value): string
{
return $this->valueSerializer->serialize($value);
}

protected function unserializeValue($serialized)
{
if (! is_string($serialized)) {
return $serialized;
}

// Attempt to unserialize the value, but return the original value if that fails.
return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized);
}

protected function shouldSetNewValue(string $key, $newValue): bool
{
if (! $this->cacheIsEnabled()) {
return true;
}

// To prevent decryption errors, we will check if we have a setting set for the current context and key.
if (! $this->doNotResetContext()->has($key)) {
return true;
}

return $newValue !== $this->doNotResetContext()->get($key);
}

public function disableCache(): self
{
$this->cacheEnabled = false;
Expand Down Expand Up @@ -297,15 +241,6 @@ public function setCache(Cache $cache): self
return $this;
}

protected function cacheIsEnabled(): bool
{
if ($this->temporarilyDisableCache) {
return false;
}

return $this->cacheEnabled && $this->cache !== null;
}

public function disableEncryption(): self
{
$this->encryptionEnabled = false;
Expand All @@ -327,11 +262,6 @@ public function setEncrypter(Encrypter $encrypter): self
return $this;
}

protected function encryptionIsEnabled(): bool
{
return $this->encryptionEnabled && $this->encrypter !== null;
}

public function enableTeams(): self
{
$this->teams = true;
Expand All @@ -358,6 +288,76 @@ public function useCacheKeyPrefix(string $prefix): self
return $this;
}

protected function normalizeKey(string $key): string
{
if (Str::startsWith(haystack: $key, needles: 'file_')) {
return str_replace(search: 'file_', replace: 'file.', subject: $key);
}

return $key;
}

protected function getCacheKey(string $key): string
{
$cacheKey = $this->cacheKeyPrefix . $key;

if ($this->teams) {
$teamId = $this->teamId ?? 'null';

$cacheKey .= "::team:{$teamId}";
}

return $cacheKey;
}

protected function getKeyForStorage(string $key): string
{
return $this->keyGenerator->generate(key: $key, context: $this->context);
}

protected function serializeValue($value): string
{
return $this->valueSerializer->serialize($value);
}

protected function unserializeValue($serialized)
{
if (! is_string($serialized)) {
return $serialized;
}

// Attempt to unserialize the value, but return the original value if that fails.
return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized);
}

protected function shouldSetNewValue(string $key, $newValue): bool
{
if (! $this->cacheIsEnabled()) {
return true;
}

// To prevent decryption errors, we will check if we have a setting set for the current context and key.
if (! $this->doNotResetContext()->has($key)) {
return true;
}

return $newValue !== $this->doNotResetContext()->get($key);
}

protected function cacheIsEnabled(): bool
{
if ($this->temporarilyDisableCache) {
return false;
}

return $this->cacheEnabled && $this->cache !== null;
}

protected function encryptionIsEnabled(): bool
{
return $this->encryptionEnabled && $this->encrypter !== null;
}

protected function decryptValue($value)
{
if (! $this->encryptionIsEnabled()) {
Expand Down
16 changes: 8 additions & 8 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function packageRegistered(): void
$this->registerSettings();
}

public function provides(): array
{
return [
Settings::class,
'SettingsFactory',
];
}

protected function bootModelBindings(): void
{
$config = $this->app['config']['settings.drivers.eloquent'];
Expand Down Expand Up @@ -70,12 +78,4 @@ protected function registerSettings(): void
return $settings;
});
}

public function provides(): array
{
return [
Settings::class,
'SettingsFactory',
];
}
}
2 changes: 1 addition & 1 deletion tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function isSerialized(mixed $data): bool
case 'd':
$end = '';

return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data);
}

return false;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\DB;
use Rawilk\Settings\Facades\Settings;
use Rawilk\Settings\Tests\TestCase;
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

class TestCase extends Orchestra
{
protected function getPackageProviders($app): array
{
return [
SettingsServiceProvider::class,
];
}

public function getEnvironmentSetUp($app): void
{
$migrations = [
Expand All @@ -27,4 +20,11 @@ public function getEnvironmentSetUp($app): void
$migration->up();
}
}

protected function getPackageProviders($app): array
{
return [
SettingsServiceProvider::class,
];
}
}

0 comments on commit ffcf3ec

Please sign in to comment.