ConfigProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 54
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventsForStatus() 0 10 3
A getTableNameByKey() 0 10 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Logistiq\Utility\Services;
6
7
use ReliqArts\Contracts\ConfigProvider as ConfigAccessor;
8
use ReliqArts\Logistiq\Utility\Contracts\ConfigProvider as ConfigProviderContract;
9
use ReliqArts\Logistiq\Utility\Exceptions\TableNameNotFound;
10
11
final class ConfigProvider implements ConfigProviderContract
12
{
13
    private const EVENT_MAP_KEY = 'event_map';
14
    private const TABLES_KEY = 'tables';
15
16
    /**
17
     * @var ConfigAccessor
18
     */
19
    private $configAccessor;
20
21
    /**
22
     * ConfigProvider constructor.
23
     *
24
     * @param ConfigAccessor $configAccessor
25
     */
26
    public function __construct(ConfigAccessor $configAccessor)
27
    {
28
        $this->configAccessor = $configAccessor;
29
    }
30
31
    /**
32
     * @param string $statusIdentifier
33
     *
34
     * @return array
35
     */
36
    public function getEventsForStatus(string $statusIdentifier): array
37
    {
38
        $eventKey = sprintf('%s.%s', self::EVENT_MAP_KEY, $statusIdentifier);
39
        $eventClasses = $this->configAccessor->get($eventKey, []);
40
41
        if (empty($eventClasses) || !is_array($eventClasses)) {
42
            return [];
43
        }
44
45
        return array_map('trim', $eventClasses);
46
    }
47
48
    /**
49
     * @param string $key
50
     *
51
     * @throws TableNameNotFound
52
     *
53
     * @return string
54
     */
55
    public function getTableNameByKey(string $key): string
56
    {
57
        $tableKey = sprintf('%s.%s', self::TABLES_KEY, $key);
58
        $tableName = $this->configAccessor->get($tableKey);
59
60
        if (empty($tableName)) {
61
            throw TableNameNotFound::forKey($key);
62
        }
63
64
        return $tableName;
65
    }
66
}
67