ServiceOptionsResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOptionsFor() 0 21 2
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