Completed
Pull Request — master (#28)
by
unknown
01:21
created

Xhgui_Saver::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * A small factory to handle creation of the profile saver instance.
4
 *
5
 * This class only exists to handle cases where an incompatible version of pimple
6
 * exists in the host application.
7
 */
8
class Xhgui_Saver
9
{
10
    /**
11
     * Get a saver instance based on configuration data.
12
     *
13
     * @param array $config The configuration data.
14
     * @return Xhgui_Saver_Interface
15
     * @throws MongoConnectionException
16
     * @throws MongoException
17
     */
18
    public static function factory($config)
19
    {
20
        switch ($config['save.handler']) {
21
            case 'file':
22
                return new Xhgui_Saver_File(
23
                    $config['save.handler.path'],
24
                    !empty($config['save.handler.filename']) ? $config['save.handler.filename'] : null,
25
                    $config['save.handler.separate_meta']
26
                );
27
28
            case 'upload':
29
                $timeout = 3;
30
                if (isset($config['save.handler.upload.timeout'])) {
31
                    $timeout = $config['save.handler.upload.timeout'];
32
                }
33
                return new Xhgui_Saver_Upload(
34
                    $config['save.handler.upload.uri'],
35
                    $timeout
36
                );
37
38
            case 'pdo':
39
                return new Xhgui_Saver_Pdo(
40
                    $config['db.dsn'],
41
                    (!empty($config['db.user'])) ? $config['db.user'] : null,
42
                    (!empty($config['db.password'])) ? $config['db.password'] : null,
43
                    $config['db.options']
44
                );
45
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
47
            case 'mongodb':
48
            default:
49
                $mongo = new MongoClient(
50
                    $config['db.host'],
51
                    $config['db.options'] +
52
                    array(
53
                        'username' => (!empty($config['db.user'])) ? $config['db.user'] : null,
54
                        'password' => (!empty($config['db.password'])) ? $config['db.password'] : null,
55
                    )
56
                );
57
58
                $collection = $mongo->{$config['db.db']}->results;
59
                $collection->findOne();
60
                return new Xhgui_Saver_Mongo($collection);
61
        }
62
    }
63
64
    /**
65
     * For usage with factory instance - for example for easier testing
66
     *
67
     * @param $config
68
     * @return Xhgui_Saver_Interface
69
     * @throws MongoConnectionException
70
     * @throws MongoException
71
     */
72
    public function create($config)
73
    {
74
        return self::factory($config);
75
    }
76
}
77