- SettingsRepository::allSettings() — inicjalizacja $settings = [] przed pętlą (bug: false ?? [] zwracało false gdy cache pusty a DB null) - Stuby wydzielone do tests/stubs/CacheHandler.php + S.php - phpunit.xml zmigurowany do schematu PHPUnit 10 (coverage → source) - composer.lock dodany do repozytorium Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
|
namespace Domain\Settings;
|
|
|
|
class SettingsRepository
|
|
{
|
|
private $db;
|
|
|
|
public function __construct($db)
|
|
{
|
|
$this->db = $db;
|
|
}
|
|
|
|
/**
|
|
* Zwraca wszystkie ustawienia jako tablicę asocjacyjną param => value.
|
|
* Wynik jest cache'owany (TTL 24h).
|
|
*/
|
|
public function allSettings(): array
|
|
{
|
|
if ( !$settings = \Shared\Cache\CacheHandler::fetch( 'settings_details' ) )
|
|
{
|
|
$settings = [];
|
|
$results = $this->db->select( 'pp_settings', '*' );
|
|
if ( is_array( $results ) )
|
|
foreach ( $results as $row )
|
|
$settings[ $row['param'] ] = $row['value'];
|
|
|
|
\Shared\Cache\CacheHandler::store( 'settings_details', $settings );
|
|
}
|
|
|
|
return $settings ?? [];
|
|
}
|
|
|
|
/**
|
|
* Upsert jednego parametru.
|
|
*/
|
|
public function update( string $param, $value ): bool
|
|
{
|
|
if ( $this->db->count( 'pp_settings', [ 'param' => $param ] ) )
|
|
return (bool) $this->db->update( 'pp_settings', [ 'value' => $value ], [ 'param' => $param ] );
|
|
else
|
|
return (bool) $this->db->insert( 'pp_settings', [ 'param' => $param, 'value' => $value ] );
|
|
}
|
|
|
|
/**
|
|
* Zapisuje zbiorczo ustawienia (TRUNCATE + INSERT).
|
|
* Czyści cache i regeneruje .htaccess.
|
|
*
|
|
* @param array $data Tablica asocjacyjna [ 'param' => value, ... ]
|
|
*/
|
|
public function save( array $data ): bool
|
|
{
|
|
$this->db->query( 'TRUNCATE pp_settings' );
|
|
|
|
$rows = [];
|
|
foreach ( $data as $param => $value )
|
|
$rows[] = [ 'param' => $param, 'value' => $value ];
|
|
|
|
$this->db->insert( 'pp_settings', $rows );
|
|
|
|
\S::delete_cache();
|
|
\S::htacces();
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Zwraca bieżącą wartość licznika odwiedzin.
|
|
*/
|
|
public function visitCounter(): ?string
|
|
{
|
|
return $this->db->get( 'pp_settings', 'value', [ 'param' => 'visits' ] ) ?: null;
|
|
}
|
|
}
|