|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Factory; |
|
17
|
|
|
|
|
18
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsService; |
|
19
|
|
|
use FRZB\Component\MetricsPower\DependencyInjection\Configuration; |
|
20
|
|
|
use FRZB\Component\MetricsPower\Enum\StorageType; |
|
21
|
|
|
use FRZB\Component\MetricsPower\Factory\Exception\NoRedisConfigurationProvidedException; |
|
22
|
|
|
use FRZB\Component\MetricsPower\Factory\Exception\NotSupportedStorageAdapterException; |
|
23
|
|
|
use Prometheus\Storage\Adapter; |
|
24
|
|
|
use Prometheus\Storage\APC; |
|
25
|
|
|
use Prometheus\Storage\APCng; |
|
26
|
|
|
use Prometheus\Storage\InMemory; |
|
27
|
|
|
use Prometheus\Storage\Redis; |
|
28
|
|
|
use Prometheus\Storage\RedisNg; |
|
29
|
|
|
|
|
30
|
|
|
#[AsService] |
|
31
|
|
|
final class PrometheusStorageAdapterFactory implements PrometheusStorageAdapterFactoryInterface |
|
32
|
|
|
{ |
|
33
|
|
|
public static function createStorageAdapter(array $configuration): Adapter |
|
34
|
|
|
{ |
|
35
|
|
|
return match (self::getStorageType($configuration)) { |
|
36
|
|
|
StorageType::Apc => new APC(), |
|
37
|
|
|
StorageType::ApcNg => new APCng(), |
|
38
|
|
|
StorageType::Redis => new Redis(self::getRedisConfiguration($configuration)), |
|
39
|
|
|
StorageType::RedisNg => new RedisNg(self::getRedisConfiguration($configuration)), |
|
40
|
|
|
StorageType::InMemory => new InMemory(), |
|
41
|
|
|
}; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @throws NotSupportedStorageAdapterException */ |
|
45
|
|
|
private static function getStorageType(array $configuration): StorageType |
|
46
|
|
|
{ |
|
47
|
|
|
$storage = $configuration['storage'] ?? StorageType::InMemory->value; |
|
48
|
|
|
|
|
49
|
|
|
return StorageType::tryFrom($storage) ?? throw NotSupportedStorageAdapterException::fromStorageType($storage); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @throws NoRedisConfigurationProvidedException */ |
|
53
|
|
|
private static function getRedisConfiguration(array $configuration): array |
|
54
|
|
|
{ |
|
55
|
|
|
return $configuration[Configuration::PROMETHEUS_REDIS] ?? throw NoRedisConfigurationProvidedException::create(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|