Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
IncentivePlanParameters
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 all
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 load
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Services;
4
5use Illuminate\Support\Facades\DB;
6
7/**
8 * FIRE-957: Thin request-memoized reader for tbl_incentive_plan_parameters.
9 * Instantiated per HTTP request from IncentivesController::__construct so
10 * one SELECT serves the whole request pipeline.
11 */
12class IncentivePlanParameters
13{
14    /** @var array<string,float>|null */
15    private ?array $cache = null;
16
17    /**
18     * Fetch a parameter value by key.
19     *
20     * @throws \RuntimeException when the key isn't seeded.
21     */
22    public function get(string $key): float
23    {
24        $this->load();
25        if (! array_key_exists($key, $this->cache)) {
26            throw new \RuntimeException("Unknown incentive plan parameter: {$key}");
27        }
28        return $this->cache[$key];
29    }
30
31    /**
32     * All parameters keyed by param_key — used by the admin-panel endpoint.
33     */
34    public function all(): array
35    {
36        $this->load();
37        return $this->cache;
38    }
39
40    private function load(): void
41    {
42        if ($this->cache !== null) {
43            return;
44        }
45        $this->cache = DB::table('tbl_incentive_plan_parameters')
46            ->pluck('value', 'param_key')
47            ->map(fn ($v) => (float) $v)
48            ->toArray();
49    }
50}