| 1 | <?php |
||
| 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 |