Completed
Pull Request — master (#2)
by Pedro
01:50
created

AbstractFactory::decorateWithConnectable()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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 2
     */
15
    protected $config;
16 2
17
    /**
18
     * @return string
19 2
     */
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
     * @return CacheProvider
44
     * @throws InvalidCacheConfigException
45
     */
46
    public function create(Config $config)
47
    {
48
        $this->config = $config;
49
50
        $cacheClassName = sprintf($config->getAdapterNamespace(), $this->config->getAdapterName());
51
52
        if (!class_exists($cacheClassName)) {
53
            throw new InvalidCacheConfig('Cache Adapter Not Supported!');
54
        }
55
56
        /** @var CacheProvider $cacheProvider */
57
        $cacheProvider = new $cacheClassName();
58
        if (!$this->isValidConfig($this->config)) {
59
60
            throw new InvalidCacheConfig('Options Not Supported Passed');
61
        }
62
63
        return $this->decorateWithConnectable($cacheProvider);
64
65
    }
66
67
    /**
68
     * @param CacheProvider $cacheProvider
69
     * @return CacheProvider
70
     */
71
    abstract protected function decorateWithConnectable(CacheProvider $cacheProvider);
72
73
    /**
74
     * @param Config $config
75
     * @return bool
76
     */
77
    abstract protected function isValidConfig(Config $config);
78
}
79