ServiceOptionsResolver::getOptionsFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
namespace FrancescoMalatesta\LaravelCircuitBreaker\Service;
4
5
use Illuminate\Config\Repository as Config;
6
7
class ServiceOptionsResolver
8
{
9
    /** @var Config */
10
    private $config;
11
12
    /**
13
     * ServiceOptionsResolver constructor.
14
     * @param Config $config
15
     */
16
    public function __construct(Config $config)
17
    {
18
        $this->config = $config;
19
    }
20
21
    /**
22
     * @param string $identifier
23
     *
24
     * @return ServiceOptions
25
     */
26
    public function getOptionsFor(string $identifier)
27
    {
28
        $defaultOptions = $this->config->get('circuit_breaker.defaults');
29
        $serviceOptionsMap = $this->config->get('circuit_breaker.services');
30
31
        if (array_key_exists($identifier, $serviceOptionsMap)) {
32
            $serviceOptions = array_merge($defaultOptions, $serviceOptionsMap[$identifier]);
33
34
            return ServiceOptions::createFromOptions(
35
                $serviceOptions['attempts_threshold'],
36
                $serviceOptions['attempts_ttl'],
37
                $serviceOptions['failure_ttl']
38
            );
39
        }
40
41
        return ServiceOptions::createFromOptions(
42
            $defaultOptions['attempts_threshold'],
43
            $defaultOptions['attempts_ttl'],
44
            $defaultOptions['failure_ttl']
45
        );
46
    }
47
}
48