ConverterCached::toRegex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 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