Completed
Push — master ( e938ca...eb16f0 )
by Patrick
02:57
created

class.MongoDataSet.php ➔ MongofillAutoload()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 4
eloc 12
c 2
b 2
f 0
nc 8
nop 1
dl 0
loc 20
rs 9.2
1
<?php
2
namespace Data;
3
4
function MongofillAutoload($classname)
5
{
6
    $classname = str_replace('/', '\\', $classname);
7
    $classname = ltrim($classname, '\\');
8
    $namespace = '';
9
    if($lastNsPos = strrpos($classname, '\\'))
10
    {
11
        $namespace = substr($classname, 0, $lastNsPos);
12
        $classname = substr($classname, $lastNsPos + 1);
13
    }
14
    if(strlen($namespace))
15
    {
16
        $namespace .= DIRECTORY_SEPARATOR;
17
    }
18
    $filename = __DIR__.'/../libs/mongofill/src/'.$namespace.$classname.'.php';
19
    if(is_readable($filename))
20
    {
21
        require $filename;
22
    }
23
}
24
25
class MongoDataSet extends DataSet
26
{
27
    protected $client;
28
    protected $manager;
29
    protected $db;
30
    protected $db_name;
31
32
    function __construct($params)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
    {
34
        $this->client = null;
35
        $this->mangaer = null;
0 ignored issues
show
Bug introduced by
The property mangaer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->db = null;
37
        $this->db_name = null;
38
        if(class_exists('MongoClient'))
39
        {
40
            $this->setupMongoClient($params);
41
        }
42
        else if(class_exists('\MongoDB\Driver\Manager'))
43
        {
44
            $this->setupMongoManager($params);
45
        }
46
        else
47
        {
48
            require __DIR__.'/../libs/mongofill/src/functions.php';
49 View Code Duplication
            if(version_compare(PHP_VERSION, '5.3.0', '>='))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            {
51
                spl_autoload_register('\Data\MongofillAutoload', true, true);
52
            }
53
            else
54
            {
55
                spl_autoload_register('\Data\MongofillAutoload');
56
            }
57
            $this->setupMongoClient($params);
58
        }
59
    }
60
61
    function tableExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        $collections = $this->db->getCollectionNames();
64
        if(in_array($name, $collections))
65
        {
66
            return true;
67
        }
68
        return false;
69
    }
70
71
    function getTable($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
72
    {
73
        if($this->db !== null)
74
        {
75
            return new MongoDataTable($this->db->selectCollection($name));
0 ignored issues
show
Bug introduced by
The method selectCollection cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
76
        }
77
        else
78
        {
79
            return new MongoDataTable($this->db_name, $name);
80
        }
81
    }
82
83
    private function setupMongoClient($params)
84
    {
85
        if($params === false)
86
        {
87
            return;
88
        }
89
        if(isset($params['user']))
90
        {
91
            $this->client = new \MongoClient('mongodb://'.$params['host'].'/'.$params['db'], array('username'=>$params['user'], 'password'=>$params['pass']));
92
        }
93
        else
94
        {
95
            $this->client = new \MongoClient('mongodb://'.$params['host'].'/'.$params['db']);
96
        }
97
        $this->db = $this->client->selectDB($params['db']);
98
    }
99
100
    private function setupMongoManager($params)
101
    {
102
        if($params === false)
103
        {
104
            return;
105
        }
106
        if(isset($params['user']))
107
        {
108
            $this->manager = new \MongoDB\Driver\Manager('mongodb://'.$params['user'].':'.$params['pass'].'@'.$params['host'].'/'.$params['db']);
109
        }
110
        else
111
        {
112
            $this->manager = new \MongoDB\Driver\Manager('mongodb://'.$params['host'].'/'.$params['db']);
113
        }
114
        $this->db_name = $params['db'];
115
    }
116
}
117
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
118