Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EngineSettings
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAuditReadEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadBoolSetting
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Engine\Infrastructure\Settings;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use PDO;
12use Throwable;
13
14/**
15 * Engine Settings.
16 *
17 * Reads engine-specific configuration values from the a_core_settings_records table.
18 * Initialized once per request via constructor injection.
19 *
20 * @package App\Core\Engine\Infrastructure\Settings
21 */
22final readonly class EngineSettings
23{
24    /**
25     * Whether READ audit logging is enabled.
26     *
27     * Driven by the engine_audit_read_log setting (0=off, 1=on).
28     *
29     * @var bool
30     */
31    private bool $auditReadEnabled;
32
33    /**
34     * EngineSettings constructor.
35     *
36     * @param PDO    $pdo    Database connection.
37     * @param string $prefix Table name prefix (defaults to 'a_').
38     */
39    public function __construct(
40        PDO $pdo,
41        private string $prefix = 'a_',
42    ) {
43        $this->auditReadEnabled = $this->loadBoolSetting($pdo, 'engine_audit_read_log');
44    }
45
46    /**
47     * Returns whether READ operation audit logging is enabled.
48     *
49     * @return bool True if read audit logging is active.
50     */
51    public function isAuditReadEnabled(): bool
52    {
53        return $this->auditReadEnabled;
54    }
55
56    /**
57     * Loads a boolean setting from the database settings table.
58     *
59     * @param PDO    $pdo Database connection.
60     * @param string $key Setting key to look up.
61     * @return bool Parsed boolean value (default false if not found).
62     */
63    private function loadBoolSetting(PDO $pdo, string $key): bool
64    {
65        try {
66            $stmt = $pdo->prepare(
67                "SELECT `setting_value` FROM `{$this->prefix}core_settings_records` WHERE `setting_key` = :key LIMIT 1"
68            );
69            $stmt->execute([':key' => $key]);
70            $value = $stmt->fetchColumn();
71
72            return $value !== false && in_array((string) $value, ['1', 'true', 'on'], true);
73        } catch (Throwable) {
74            return false;
75        }
76    }
77}