Completed
Pull Request — master (#14)
by Pavel
05:45
created

CacheConfiguration   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 99
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getStrategy() 0 4 2
A create() 0 16 2
A disabled() 0 7 1
A getTtl() 0 4 2
A isEnabled() 0 4 1
A extra() 0 12 3
A configureResolver() 0 11 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Cache;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class CacheConfiguration
8
{
9
    /** @var int|null */
10
    private $ttl;
11
    /** @var bool */
12
    private $enabled = false;
13
    /** @var KeyStrategyInterface */
14
    private $strategy;
15
    /** @var  array */
16
    private $extra;
17
18 17
    private function __construct()
19
    {
20 17
    }
21
22
    /**
23
     * @return KeyStrategyInterface
24
     */
25 1
    public function getStrategy()
26
    {
27 1
        return $this->enabled ? $this->strategy : null;
28
    }
29
30
    /**
31
     * @param array $data
32
     *
33
     * @return static
34
     */
35 1
    public static function create(array $data)
36
    {
37 1
        $resolver = new OptionsResolver();
38 1
        self::configureResolver($resolver);
39 1
        $data = $resolver->resolve($data);
40
41 1
        $configuration = new static();
42
43 1
        $configuration->enabled  = $data['enabled'];
44 1
        $configuration->strategy = $data['strategy'];
45 1
        $configuration->ttl      = $data['ttl'];
46
47 1
        $configuration->extra = $data['extra'] ?: [];
48
49 1
        return $configuration;
50
    }
51
52
    /**
53
     * Constructor for disabled cache entities configuration
54
     *
55
     * @return static
56
     */
57 16
    public static function disabled()
58
    {
59 16
        $configuration          = new static();
60 16
        $configuration->enabled = false;
61
62 16
        return $configuration;
63
    }
64
65
    /**
66
     * @return int|null
67
     */
68 1
    public function getTtl()
69
    {
70 1
        return $this->enabled ? $this->ttl : null;
71
    }
72
73
    /**
74
     * @return boolean
75
     */
76 17
    public function isEnabled()
77
    {
78 17
        return $this->enabled;
79
    }
80
81 1
    public function extra($key)
82
    {
83 1
        if (!$this->enabled) {
84
            return null;
85
        }
86
87 1
        if (!array_key_exists($key, $this->extra)) {
88
            return null;
89
        }
90
91 1
        return $this->extra[$key];
92
    }
93
94 1
    private static function configureResolver(OptionsResolver $resolver)
95
    {
96 1
        $resolver->setDefault('enabled', false);
97 1
        $resolver->setAllowedValues('enabled', [false, true]);
98 1
        $resolver->setDefault('ttl', null);
99 1
        $resolver->setAllowedTypes('ttl', ['int', 'null']);
100 1
        $resolver->setDefault('strategy', new ScalarKeyStrategy());
101 1
        $resolver->setAllowedTypes('strategy', KeyStrategyInterface::class);
102 1
        $resolver->setDefault('extra', []);
103 1
        $resolver->setAllowedTypes('extra', ['array', null]);
104 1
    }
105
}
106