AbstractFactory::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
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 1
    public function __construct()
23
    {
24 1
        if (!$this->moduleIsInstalled()) {
25 1
            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 4
    public function create(Config $config)
49
    {
50 4
        $this->config = $config;
51
52 4
        $cacheClassName = sprintf($config->getAdapterNamespace(), $this->config->getAdapterName());
53
54 4
        if (!class_exists($cacheClassName)) {
55
            throw new InvalidCacheConfig('Cache Adapter Not Supported!');
56
        }
57
58
        /** @var CacheProvider $cacheProvider */
59 4
        $cacheProvider = new $cacheClassName();
60 4
        if (!$this->isValidConfig($this->config)) {
61
            throw new InvalidCacheConfig('Options Not Supported Passed');
62
        }
63
64 4
        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