Completed
Pull Request — master (#315)
by Elan
01:14
created

Xhgui_Saver::factory()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
use MongoDB\Driver\Manager;
4
5
/**
6
 * A small factory to handle creation of the profile saver instance.
7
 *
8
 * This class only exists to handle cases where an incompatible version of pimple
9
 * exists in the host application.
10
 */
11
class Xhgui_Saver
12
{
13
    /**
14
     * Get a saver instance based on configuration data.
15
     *
16
     * @param array $config The configuration data.
17
     * @return Xhgui_Saver_Interface
18
     */
19
    public static function factory($config)
20
    {
21
        switch ($config['save.handler']) {
22
            case 'pdo':
23
                if (!class_exists(PDO::class)) {
24
                    throw new RuntimeException("Required extension ext-pdo missing");
25
                }
26
                return new Xhgui_Saver_Pdo(
27
                    new PDO(
28
                        $config['pdo']['dsn'],
29
                        $config['pdo']['user'],
30
                        $config['pdo']['pass']
31
                    ),
32
                    $config['pdo']['table']
33
                );
34
35
            case 'mongodb':
36
                if (!class_exists(Manager::class)) {
37
                    throw new RuntimeException("Required extension ext-mongodb missing");
38
                }
39
                $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']);
40
                $collection = $mongo->{$config['db.db']}->results;
41
                $collection->findOne();
42
                return new Xhgui_Saver_Mongo($collection);
43
44
            default:
45
                throw new RuntimeException("Unsupported save handler: {$config['save.handler']}");
46
        }
47
    }
48
}
49