CoreConfig::manFloat()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\Config;
5
6
use Plaisio\Kernel\Nub;
7
use SetBased\Exception\RuntimeException;
8
use SetBased\Helper\Cast;
9
10
/**
11
 * Class for fetching values of configuration parameters.
12
 */
13
class CoreConfig implements Config
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * @inheritDoc
18
   */
19
  public function manArray(int $cfgId): array
20
  {
21
    try
22
    {
23
      $json  = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
24
      $value = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
25
      if (!is_array($value))
26
      {
27
        throw new RuntimeException('Value of parameter %d is not an array', $cfgId);
28
      }
29
30
      return $value;
31
    }
32
    catch (\Throwable $exception)
33
    {
34
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
35
                                 $cfgId,
36
                                 $exception->getMessage());
37
    }
38
  }
39
40
  //--------------------------------------------------------------------------------------------------------------------
41
  /**
42
   * @inheritDoc
43
   */
44 3
  public function manBool(int $cfgId): bool
45
  {
46
    try
47
    {
48 3
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
49
50 3
      return Cast::toManBool($value);
51
    }
52 2
    catch (\Throwable $exception)
53
    {
54 2
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
55
                                 $cfgId,
56 2
                                 $exception->getMessage());
57
    }
58
  }
59
60
  //--------------------------------------------------------------------------------------------------------------------
61
  /**
62
   * @inheritDoc
63
   */
64 1
  public function manFiniteFloat(int $cfgId): float
65
  {
66
    try
67
    {
68 1
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
69
70 1
      return Cast::toManFiniteFloat($value);
71
    }
72
    catch (\Throwable $exception)
73
    {
74
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
75
                                 $cfgId,
76
                                 $exception->getMessage());
77
    }
78
  }
79
80
  //--------------------------------------------------------------------------------------------------------------------
81
  /**
82
   * @inheritDoc
83
   */
84 3
  public function manFloat(int $cfgId): float
85
  {
86
    try
87
    {
88 3
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
89
90 3
      return Cast::toManFloat($value);
91
    }
92 2
    catch (\Throwable $exception)
93
    {
94 2
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
95
                                 $cfgId,
96 2
                                 $exception->getMessage());
97
    }
98
  }
99
100
  //--------------------------------------------------------------------------------------------------------------------
101
  /**
102
   * @inheritDoc
103
   */
104 3
  public function manInt(int $cfgId): int
105
  {
106
    try
107
    {
108 3
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
109
110 3
      return Cast::toManInt($value);
111
    }
112 2
    catch (\Throwable $exception)
113
    {
114 2
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
115
                                 $cfgId,
116 2
                                 $exception->getMessage());
117
    }
118
  }
119
120
  //--------------------------------------------------------------------------------------------------------------------
121
  /**
122
   * @inheritDoc
123
   */
124 2
  public function manString(int $cfgId): string
125
  {
126
    try
127
    {
128 2
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
129
130 2
      return Cast::toManString($value);
131
    }
132 1
    catch (\Throwable $exception)
133
    {
134 1
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
135
                                 $cfgId,
136 1
                                 $exception->getMessage());
137
    }
138
  }
139
140
  //--------------------------------------------------------------------------------------------------------------------
141
  /**
142
   * @inheritDoc
143
   */
144
  public function optArray(int $cfgId): ?array
145
  {
146
    try
147
    {
148
      $json  = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
149
      $value = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
150
      if (!is_array($value) && $value!==null)
151
      {
152
        throw new RuntimeException('Value if parameter %d is not an array nor null', $cfgId);
153
      }
154
155
      return $value;
156
    }
157
    catch (\Throwable $exception)
158
    {
159
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
160
                                 $cfgId,
161
                                 $exception->getMessage());
162
    }
163
  }
164
165
  //--------------------------------------------------------------------------------------------------------------------
166
  /**
167
   * @inheritDoc
168
   */
169 2
  public function optBool(int $cfgId): ?bool
170
  {
171
    try
172
    {
173 2
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
174
175 2
      return Cast::toOptBool($value);
176
    }
177 1
    catch (\Throwable $exception)
178
    {
179 1
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
180
                                 $cfgId,
181 1
                                 $exception->getMessage());
182
    }
183
  }
184
185
  //--------------------------------------------------------------------------------------------------------------------
186
  /**
187
   * @inheritDoc
188
   */
189 1
  public function optFiniteFloat(int $cfgId): ?float
190
  {
191
    try
192
    {
193 1
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
194
195 1
      return Cast::toOptFiniteFloat($value);
196
    }
197
    catch (\Throwable $exception)
198
    {
199
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
200
                                 $cfgId,
201
                                 $exception->getMessage());
202
    }
203
  }
204
205
  //--------------------------------------------------------------------------------------------------------------------
206
  /**
207
   * @inheritDoc
208
   */
209 1
  public function optFloat(int $cfgId): ?float
210
  {
211
    try
212
    {
213 1
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
214
215 1
      return Cast::toOptFloat($value);
216
    }
217 1
    catch (\Throwable $exception)
218
    {
219 1
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
220
                                 $cfgId,
221 1
                                 $exception->getMessage());
222
    }
223
  }
224
225
  //--------------------------------------------------------------------------------------------------------------------
226
  /**
227
   * @inheritDoc
228
   */
229 2
  public function optInt(int $cfgId): ?int
230
  {
231
    try
232
    {
233 2
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
234
235 2
      return Cast::toOptInt($value);
236
    }
237 1
    catch (\Throwable $exception)
238
    {
239 1
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
240
                                 $cfgId,
241 1
                                 $exception->getMessage());
242
    }
243
  }
244
245
  //--------------------------------------------------------------------------------------------------------------------
246
  /**
247
   * @inheritDoc
248
   */
249 1
  public function optString(int $cfgId): ?string
250
  {
251
    try
252
    {
253 1
      $value = Nub::$nub->DL->abcConfigCoreGetValue(Nub::$nub->companyResolver->getCmpId(), $cfgId);
254
255 1
      return Cast::toOptString($value);
256
    }
257
    catch (\Throwable $exception)
258
    {
259
      throw new RuntimeException([$exception], 'Failed to fetch value of parameter %d: %s',
260
                                 $cfgId,
261
                                 $exception->getMessage());
262
    }
263
  }
264
265
  //--------------------------------------------------------------------------------------------------------------------
266
}
267
268
//----------------------------------------------------------------------------------------------------------------------
269