Completed
Push — master ( a7744a...b1ae65 )
by Patrick
04:12
created

ConfigProvider::provide()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
4
namespace PFlorek\AwsParameterStore;
5
6
7
use Aws\Ssm\SsmClient;
8
use function PFlorek\Elevator\array_elevate;
9
10
class ConfigProvider
11
{
12
    /**
13
     * @var Options
14
     */
15
    private $options;
16
17
    /**
18
     * @var Reader
19
     */
20
    private $reader;
21
22
    /**
23
     * @param array|SsmClient|Reader $client
24
     * @param Options|string[] $options
25
     */
26
    public function __construct($client, $options)
27
    {
28
        if (is_array($options)) {
29
            $options = Options::create($options);
30
        }
31
        $this->options = $options;
32
33
        if(!$client instanceof Reader) {
34
            $client = new Reader($client);
35
        }
36
        $this->reader = $client;
37
    }
38
39
    /**
40
     * @param string|string[] $profile
41
     * @return string[]|int[]|float[]|bool[]
42
     */
43
    public function __invoke($profile = []): array
44
    {
45
        if (!is_array($profile)) {
46
            $profile = (array)$profile;
47
        }
48
        $paths = $this->createPaths($profile);
0 ignored issues
show
Bug introduced by
It seems like $profile can also be of type string; however, parameter $profiles of PFlorek\AwsParameterStor...Provider::createPaths() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $paths = $this->createPaths(/** @scrutinizer ignore-type */ $profile);
Loading history...
49
        $paths = array_reverse($paths);
50
51
        $config = [];
52
        foreach ($paths as $context) {
53
            $config += $this->reader->fromPath($context);
54
        }
55
56
        $config = array_map([SimpleCast::class, 'cast'], $config);
57
58
        return array_elevate($config);
59
    }
60
61
    /**
62
     * @param string[] $profiles
63
     * @return string[]
64
     */
65
    private function createPaths(array $profiles): array
66
    {
67
        $prefix = $this->options->getPrefix();
68
        $prefix = trim($prefix, '/');
69
70
        $sharedContext = $this->options->getSharedContext();
71
        $sharedContext = trim($sharedContext, '/');
72
73
        $name = $this->options->getApplicationName();
74
        $name = trim($name, '/');
75
76
        $separator = $this->options->getProfileSeparator();
77
78
        $paths = [];
79
        if ($sharedContext) {
80
            $paths[] = "/{$prefix}/{$sharedContext}";
81
        }
82
83
        $paths[] = "/{$prefix}/{$name}"; // base context
84
85
        foreach ($profiles as $profile) {
86
            $profile = trim($profile, '/');
87
            $paths[] = "/{$prefix}/{$name}{$separator}{$profile}";
88
        }
89
90
        return $paths;
91
    }
92
}