ConverterCached   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getKey() 0 4 1
A toRegex() 0 13 2
1
<?php
2
3
namespace HansOtt\RangeRegex;
4
5
final class ConverterCached implements Converter
6
{
7
    private $converter;
8
9
    private $cache = [];
10
11 4
    public function __construct(Converter $converter)
12
    {
13 4
        $this->converter = $converter;
14 4
    }
15
16 2
    private function getKey(Range $range)
17
    {
18 2
        return sprintf('%d:%d', $range->getMin(), $range->getMax());
19
    }
20
21 2
    public function toRegex(Range $range)
22
    {
23 2
        $key = $this->getKey($range);
24
25 2
        if (isset($this->cache[$key])) {
26 1
            return $this->cache[$key];
27
        }
28
29 2
        $regex = $this->converter->toRegex($range);
30 2
        $this->cache[$key] = $regex;
31
32 2
        return $regex;
33
    }
34
}
35