Completed
Push — master ( 539dc8...66ab79 )
by Mr
03:11
created

Mongo::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace DrMVC\Database\Drivers;
4
5
use DrMVC\Database\Database;
6
use MongoDB\BSON\ObjectID;
7
8
/**
9
 * Class for work with modern MongoDB php driver (for PHP >= 7.0)
10
 * @package DrMVC\Database\Drivers
11
 */
12
class Mongo extends NoSQL
13
{
14
//    /**
15
//     * DMongoDB constructor
16
//     *
17
//     * @param string $name
18
//     * @param array $config
19
//     */
20
//    public function __construct($name, array $config)
21
//    {
22
//        parent::__construct($name, $config);
23
//
24
//        // Make sure the database is connected
25
//        $this->_connection or $this->connect();
26
//    }
27
//
28
//    /**
29
//     * Connect via MongoClient driver
30
//     */
31
//    public function connect()
32
//    {
33
//        if ($this->_connection) return;
34
//
35
//        // Auth to database
36
//        $userpass = isset($this->_config['username']) && isset($this->_config['password']) ? $this->_config['username'] . ':' . $this->_config['password'] . '@' : null;
37
//
38
//        // Check if we want to authenticate against a specific database.
39
//        $auth_database = isset($this->_config['options']) && !empty($this->_config['options']['database']) ? $this->_config['options']['database'] : null;
40
//
41
//        // Set connection
42
//        $this->_connection = new \MongoDB\Driver\Manager('mongodb://' . $userpass . $this->_config['hostname'] . ':' . $this->_config['port'] . ($auth_database ? '/' . $auth_database : ''));
43
//    }
44
//
45
//    /**
46
//     * Check if incoming hash is valid mongo object id hash
47
//     *
48
//     * @param $value
49
//     * @return bool
50
//     */
51
//    function isValid($value)
52
//    {
53
//        if ($value instanceof ObjectID) {
54
//            return true;
55
//        }
56
//        try {
57
//            new ObjectID($value);
58
//            return true;
59
//        } catch (\Exception $e) {
60
//            return false;
61
//        }
62
//    }
63
//
64
//    /**
65
//     * Write into database
66
//     *
67
//     * @param $collection
68
//     * @param $command
69
//     * @param $data
70
//     * @return mixed
71
//     */
72
//    public function write($collection, $command, $data)
73
//    {
74
//        // Set the last query
75
//        $this->_last_query = $data;
76
//
77
//        // Exec bulk command
78
//        $bulk = new \MongoDB\Driver\BulkWrite();
79
//
80
//        switch ($command) {
81
//            case 'insert':
82
//                $data['_id'] = new \MongoDB\BSON\ObjectID;
83
//                $bulk->insert($data);
84
//                break;
85
//            case 'update';
86
//                $bulk->update($data[0], $data[1], $data[2]);
87
//                break;
88
//            case 'delete';
89
//                $bulk->delete($data[0], $data[1]);
90
//                break;
91
//        }
92
//
93
//        try {
94
//            $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
95
//            $this->_connection->executeBulkWrite($this->_config['database'] . '.' . $collection, $bulk, $writeConcern);
96
//            if ($command == 'insert') $response = (string) new \MongoDB\BSON\ObjectID($data['_id']); else $response = true;
97
//        } catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
98
//            error_log(
99
//                "Uncaught Error: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine() . "\n"
100
//                . "Stack trace:\n" . $e->getTraceAsString() . "\n"
101
//                . "\tthrown in " . $e->getFile() . " on line " . $e->getLine()
102
//            );
103
//            exit;
104
//        }
105
//
106
//        return $response;
107
//    }
108
//
109
//    /**
110
//     * Execute MongoCommand
111
//     *
112
//     * @param $query - Should be like new MongoDB\Driver\Query($filter, $options);
113
//     * @return mixed
114
//     */
115
//    public function command($query)
116
//    {
117
//        // Set the last query
118
//        $this->_last_query = $query;
119
//
120
//        // Create command from query
121
//        $command = new \MongoDB\Driver\Command($query);
122
//
123
//        try {
124
//            $cursor = $this->_connection->executeCommand($this->_config['database'], $command);
125
//            $response = $cursor->toArray();
126
//        } catch (\MongoDB\Driver\Exception\Exception $e) {
127
//            error_log(
128
//                "Uncaught Error: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine() . "\n"
129
//                . "Stack trace:\n" . $e->getTraceAsString() . "\n"
130
//                . "\tthrown in " . $e->getFile() . " on line " . $e->getLine()
131
//            );
132
//            exit;
133
//        }
134
//
135
//        return $response;
136
//    }
137
//
138
//    /**
139
//     * Execute MongoQuery
140
//     *
141
//     * @param $collection
142
//     * @param $filter
143
//     * @param $options
144
//     * @return mixed
145
//     */
146
//    public function query($collection, $filter, $options)
147
//    {
148
//        // Set the last query
149
//        $this->_last_query = array($collection, $filter, $options);
150
//
151
//        // Create command from query
152
//        $query = new \MongoDB\Driver\Query($filter, $options);
153
//
154
//        try {
155
//            $cursor = $this->_connection->executeQuery($this->_config['database'] . '.' . $collection, $query);
156
//            $response = $cursor->toArray();
157
//        } catch (\MongoDB\Driver\Exception\Exception $e) {
158
//            error_log(
159
//                "Uncaught Error: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine() . "\n"
160
//                . "Stack trace:\n" . $e->getTraceAsString() . "\n"
161
//                . "\tthrown in " . $e->getFile() . " on line " . $e->getLine()
162
//            );
163
//            exit;
164
//        }
165
//
166
//        return $response;
167
//    }
168
}
169