ConverterCached::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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