1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SimpleSAML\Module\statistics; |
||
6 | |||
7 | use Exception; |
||
8 | use SimpleSAML\Configuration; |
||
9 | use SimpleSAML\Module; |
||
10 | use SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule; |
||
11 | |||
12 | /** |
||
13 | * @package SimpleSAMLphp |
||
14 | */ |
||
15 | class Ruleset |
||
16 | { |
||
17 | /** @var \SimpleSAML\Configuration */ |
||
18 | private Configuration $statconfig; |
||
19 | |||
20 | /** @var array */ |
||
21 | private array $availrulenames; |
||
22 | |||
23 | /** @var array */ |
||
24 | private array $availrules; |
||
25 | |||
26 | /** @var array */ |
||
27 | private array $available; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param \SimpleSAML\Configuration $statconfig |
||
34 | */ |
||
35 | public function __construct(Configuration $statconfig) |
||
36 | { |
||
37 | $this->statconfig = $statconfig; |
||
38 | $this->init(); |
||
39 | } |
||
40 | |||
41 | |||
42 | /** |
||
43 | */ |
||
44 | private function init(): void |
||
45 | { |
||
46 | $statdir = $this->statconfig->getValue('statdir'); |
||
47 | $statrules = $this->statconfig->getValue('statrules'); |
||
48 | $timeres = $this->statconfig->getValue('timeres'); |
||
49 | |||
50 | /* |
||
51 | * Walk through file lists, and get available [rule][fileslot]... |
||
52 | */ |
||
53 | if (!is_dir($statdir)) { |
||
54 | throw new Exception('Statisics output directory [' . $statdir . '] does not exist.'); |
||
55 | } |
||
56 | $filelist = scandir($statdir); |
||
57 | $this->available = []; |
||
58 | foreach ($filelist as $file) { |
||
59 | if (preg_match('/([a-z0-9_]+)-([a-z0-9_]+)-([0-9]+)\.stat/', $file, $matches)) { |
||
60 | if (array_key_exists($matches[1], $statrules)) { |
||
61 | if (array_key_exists($matches[2], $timeres)) { |
||
62 | $this->available[$matches[1]][$matches[2]][] = $matches[3]; |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | if (empty($this->available)) { |
||
68 | throw new Exception('No aggregated statistics files found in [' . $statdir . ']'); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Create array with information about available rules.. |
||
73 | */ |
||
74 | $this->availrules = array_keys($statrules); |
||
75 | $available_rules = []; |
||
76 | foreach ($this->availrules as $key) { |
||
77 | $available_rules[$key] = ['name' => $statrules[$key]['name'], 'descr' => $statrules[$key]['descr']]; |
||
78 | } |
||
79 | $this->availrulenames = $available_rules; |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | public function availableRules(): array |
||
87 | { |
||
88 | return $this->availrules; |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | public function availableRulesNames(): array |
||
96 | { |
||
97 | return $this->availrulenames; |
||
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Resolve which rule is selected. Taking user preference and checks if it exists. |
||
103 | * |
||
104 | * @param string|null $preferRule |
||
105 | * @return string|null |
||
106 | */ |
||
107 | private function resolveSelectedRule(?string $preferRule = null): ?string |
||
108 | { |
||
109 | $rule = $this->statconfig->getOptionalString('default', $this->availrules[0]); |
||
110 | if (!empty($preferRule)) { |
||
111 | if (in_array($preferRule, $this->availrules, true)) { |
||
112 | $rule = $preferRule; |
||
113 | } |
||
114 | } |
||
115 | return $rule; |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * @param string|null $preferRule |
||
121 | * @return \SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule |
||
122 | */ |
||
123 | public function getRule(?string $preferRule = null): BaseRule |
||
124 | { |
||
125 | $rule = $this->resolveSelectedRule($preferRule); |
||
126 | $statrulesConfig = $this->statconfig->getConfigItem('statrules'); |
||
127 | $statruleConfig = $statrulesConfig->getConfigItem($rule); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
128 | |||
129 | $presenterClass = Module::resolveClass( |
||
130 | $statruleConfig->getOptionalValue('presenter', 'statistics:BaseRule'), |
||
131 | 'Statistics\Rulesets', |
||
132 | ); |
||
133 | |||
134 | /** @psalm-suppress InvalidStringClass */ |
||
135 | $statrule = new $presenterClass($this->statconfig, $statruleConfig, $rule, $this->available); |
||
136 | |||
137 | /** @var \SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule $statrule */ |
||
138 | return $statrule; |
||
139 | } |
||
140 | } |
||
141 |