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

Xhgui_Saver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B factory() 0 44 10
A create() 0 4 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.filename'],
24
                    $config['save.handler.separate_meta']
25
                );
26
27
            case 'upload':
28
                $timeout = 3;
29
                if (isset($config['save.handler.upload.timeout'])) {
30
                    $timeout = $config['save.handler.upload.timeout'];
31
                }
32
                return new Xhgui_Saver_Upload(
33
                    $config['save.handler.upload.uri'],
34
                    $timeout
35
                );
36
37
            case 'pdo':
38
                return new Xhgui_Saver_PDO(
39
                    $config['db.dsn'],
40
                    (!empty($config['db.user'])) ? $config['db.user'] : null,
41
                    (!empty($config['db.password'])) ? $config['db.password'] : null,
42
                    $config['db.options']
43
                );
44
                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...
45
46
            case 'mongodb':
47
            default:
48
                $mongo = new MongoClient(
49
                    $config['db.host'],
50
                    $config['db.options'] +
51
                    [
52
                        'username' => (!empty($config['db.user'])) ? $config['db.user'] : null,
53
                        'password' => (!empty($config['db.password'])) ? $config['db.password'] : null,
54
                    ]
55
                );
56
57
                $collection = $mongo->{$config['db.db']}->results;
58
                $collection->findOne();
59
                return new Xhgui_Saver_Mongo($collection);
60
        }
61
    }
62
63
    /**
64
     * For usage with factory instance - for example for easier testing
65
     *
66
     * @param $config
67
     * @return Xhgui_Saver_Interface
68
     * @throws MongoConnectionException
69
     * @throws MongoException
70
     */
71
    public function create($config)
72
    {
73
        return self::factory($config);
74
    }
75
}
76