Completed
Push — master ( 9da291...9cb0b0 )
by Pedro
9s
created

AbstractFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 41.18%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 1
cbo 3
dl 0
loc 71
ccs 7
cts 17
cp 0.4118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
getModuleName() 0 1 ?
decorateWithConnectable() 0 1 ?
isValidConfig() 0 1 ?
A __construct() 0 6 2
A moduleIsInstalled() 0 8 2
A create() 0 18 3
1
<?php
2
3
namespace Pcelta\Doctrine\Cache\Factory;
4
5
use Pcelta\Doctrine\Cache\Entity\Config;
6
use Pcelta\Doctrine\Cache\Exception\InvalidCacheConfig;
7
use Pcelta\Doctrine\Cache\Exception\ModuleIsNotInstalled;
8
use Doctrine\Common\Cache\CacheProvider;
9
10
abstract class AbstractFactory implements Factorable
11
{
12
    /**
13
     * @var Config
14
     */
15
    protected $config;
16
17
    /**
18
     * @return string
19
     */
20
    abstract public function getModuleName();
21
22
    public function __construct()
23
    {
24
        if (!$this->moduleIsInstalled()) {
25
            throw new ModuleIsNotInstalled($this->getModuleName());
26
        }
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    public function moduleIsInstalled()
33
    {
34
        if (!extension_loaded($this->getModuleName())) {
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    /**
42
     * @param Config $config
43
     *
44
     * @return CacheProvider
45
     *
46
     * @throws InvalidCacheConfigException
47
     */
48 5
    public function create(Config $config)
49
    {
50 5
        $this->config = $config;
51
52 5
        $cacheClassName = sprintf($config->getAdapterNamespace(), $this->config->getAdapterName());
53
54 5
        if (!class_exists($cacheClassName)) {
55
            throw new InvalidCacheConfig('Cache Adapter Not Supported!');
56
        }
57
58
        /** @var CacheProvider $cacheProvider */
59 5
        $cacheProvider = new $cacheClassName();
60 5
        if (!$this->isValidConfig($this->config)) {
61
            throw new InvalidCacheConfig('Options Not Supported Passed');
62
        }
63
64 5
        return $this->decorateWithConnectable($cacheProvider);
65
    }
66
67
    /**
68
     * @param CacheProvider $cacheProvider
69
     *
70
     * @return CacheProvider
71
     */
72
    abstract protected function decorateWithConnectable(CacheProvider $cacheProvider);
73
74
    /**
75
     * @param Config $config
76
     *
77
     * @return bool
78
     */
79
    abstract protected function isValidConfig(Config $config);
80
}
81