Completed
Push — master ( 02d26c...304629 )
by Walter
03:17
created

StorageFactory::createService()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.439
cc 5
eloc 19
nc 8
nop 1
1
<?php
2
3
namespace PhpAbModule\Service;
4
5
use PhpAb\Engine\Engine;
6
use PhpAb\Storage\Cookie;
7
use PhpAb\Storage\Runtime;
8
use PhpAb\Storage\Session;
9
use RuntimeException;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
class StorageFactory implements FactoryInterface
14
{
15
    public function createService(ServiceLocatorInterface $serviceLocator)
16
    {
17
        $config = $serviceLocator->get('Config');
18
19
        if (array_key_exists('storage_options', $config['phpab'])) {
20
            $options = $config['phpab']['storage_options'];
21
        } else {
22
            $options = [];
23
        }
24
25
        switch ($config['phpab']['storage']) {
26
            case 'cookie':
27
                $storage = new Cookie($options['name'], $options['ttl']);
28
                break;
29
30
            case 'runtime':
31
                $storage = new Runtime();
32
                break;
33
34
            case 'session':
35
                $storage = new Session($options['name']);
36
                break;
37
38
            default:
39
                throw new RuntimeException('Invalid storage provider set: ' . $config['phpab']['storage']);
40
        }
41
42
        return $storage;
43
    }
44
}
45