Passed
Pull Request — master (#14)
by Pavel
03:16
created

CacheConfiguration   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
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 94
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 implements CacheConfigurationInterface
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 24
    private function __construct()
19
    {
20 24
    }
21
22
    /** {@inheritdoc} */
23 1
    public function getStrategy()
24
    {
25 1
        return $this->enabled ? $this->strategy : null;
26
    }
27
28
    /**
29
     * @param array $data
30
     *
31
     * @return static
32
     */
33 1
    public static function create(array $data)
34
    {
35 1
        $resolver = new OptionsResolver();
36 1
        self::configureResolver($resolver);
37 1
        $data = $resolver->resolve($data);
38
39 1
        $configuration = new static();
40
41 1
        $configuration->enabled  = $data['enabled'];
42 1
        $configuration->strategy = $data['strategy'];
43 1
        $configuration->ttl      = $data['ttl'];
44
45 1
        $configuration->extra = $data['extra'] ?: [];
46
47 1
        return $configuration;
48
    }
49
50
    /**
51
     * Constructor for disabled cache entities configuration
52
     *
53
     * @return static
54
     */
55 23
    public static function disabled()
56
    {
57 23
        $configuration          = new static();
58 23
        $configuration->enabled = false;
59
60 23
        return $configuration;
61
    }
62
63
    /** {@inheritdoc} */
64 1
    public function getTtl()
65
    {
66 1
        return $this->enabled ? $this->ttl : null;
67
    }
68
69
    /** {@inheritdoc} */
70 24
    public function isEnabled()
71
    {
72 24
        return $this->enabled;
73
    }
74
75
    /** {@inheritdoc} */
76 1
    public function extra($key)
77
    {
78 1
        if (!$this->enabled) {
79
            return null;
80
        }
81
82 1
        if (!array_key_exists($key, $this->extra)) {
83
            return null;
84
        }
85
86 1
        return $this->extra[$key];
87
    }
88
89 1
    private static function configureResolver(OptionsResolver $resolver)
90
    {
91 1
        $resolver->setDefault('enabled', false);
92 1
        $resolver->setAllowedValues('enabled', [false, true]);
93 1
        $resolver->setDefault('ttl', null);
94 1
        $resolver->setAllowedTypes('ttl', ['int', 'null']);
95 1
        $resolver->setDefault('strategy', new ScalarKeyStrategy());
96 1
        $resolver->setAllowedTypes('strategy', KeyStrategyInterface::class);
97 1
        $resolver->setDefault('extra', []);
98 1
        $resolver->setAllowedTypes('extra', ['array', null]);
99 1
    }
100
}
101