Completed
Pull Request — master (#8)
by
unknown
02:47
created

MongoClient::getWriteConcern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
use Alcaeus\MongoDbAdapter\Helper;
17
use MongoDB\Client;
18
19
/**
20
 * A connection between PHP and MongoDB. This class is used to create and manage connections
21
 * See MongoClient::__construct() and the section on connecting for more information about creating connections.
22
 * @link http://www.php.net/manual/en/class.mongoclient.php
23
 */
24
class MongoClient
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
25
{
26
    use Helper\ReadPreference;
27
    use Helper\WriteConcern;
28
    
29
30
    const VERSION = '1.6.12';
31
    const DEFAULT_HOST = "localhost" ;
32
    const DEFAULT_PORT = 27017 ;
33
    const RP_PRIMARY = "primary" ;
34
    const RP_PRIMARY_PREFERRED = "primaryPreferred" ;
35
    const RP_SECONDARY = "secondary" ;
36
    const RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
37
    const RP_NEAREST = "nearest" ;
38
39
    /**
40
     * @var bool
41
     * @deprecated This will not properly work as the underlying driver connects lazily
42
     */
43
    public $connected = false;
44
45
    /**
46
     * @var
47
     */
48
    public $status;
49
50
    /**
51
     * @var string
52
     */
53
    protected $server;
54
55
    /**
56
     * @var
57
     */
58
    protected $persistent;
59
60
    /**
61
     * @var Client
62
     */
63
    private $client;
64
65
    /**
66
     * Creates a new database connection object
67
     *
68
     * @link http://php.net/manual/en/mongo.construct.php
69
     * @param string $server The server name.
70
     * @param array $options An array of options for the connection.
71
     * @param array $driverOptions An array of options for the MongoDB driver.
72
     * @throws MongoConnectionException
73
     */
74
    public function __construct($server = 'default', array $options = ["connect" => true], array $driverOptions = [])
75
    {
76
        if ($server === 'default') {
77
            $server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT;
78
        }
79
80
        $this->server = $server;
81
        $this->client = new Client($server, $options, $driverOptions);
82
        $this->readPreference = new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY);
83
84
        if (isset($options['connect']) && $options['connect']) {
85
            $this->connect();
86
        }
87
    }
88
89
    /**
90
     * Closes this database connection
91
     *
92
     * @link http://www.php.net/manual/en/mongoclient.close.php
93
     * @param  boolean|string $connection
94
     * @return boolean If the connection was successfully closed.
95
     */
96
    public function close($connection = null)
0 ignored issues
show
Unused Code introduced by
The parameter $connection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    {
98
        $this->connected = false;
1 ignored issue
show
Deprecated Code introduced by
The property MongoClient::$connected has been deprecated with message: This will not properly work as the underlying driver connects lazily

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
99
100
        return false;
101
    }
102
103
    /**
104
     * Connects to a database server
105
     *
106
     * @link http://www.php.net/manual/en/mongoclient.connect.php
107
     *
108
     * @throws MongoConnectionException
109
     * @return boolean If the connection was successful.
110
     */
111
    public function connect()
112
    {
113
        $this->connected = true;
1 ignored issue
show
Deprecated Code introduced by
The property MongoClient::$connected has been deprecated with message: This will not properly work as the underlying driver connects lazily

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
114
115
        return true;
116
    }
117
118
    /**
119
     * Drops a database
120
     *
121
     * @link http://www.php.net/manual/en/mongoclient.dropdb.php
122
     * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database.
123
     * @return array The database response.
124
     * @deprecated Use MongoDB::drop() instead.
125
     */
126
    public function dropDB($db)
127
    {
128
        return $this->selectDB($db)->drop();
129
    }
130
131
    /**
132
     * Gets a database
133
     *
134
     * @link http://php.net/manual/en/mongoclient.get.php
135
     * @param string $dbname The database name.
136
     * @return MongoDB The database name.
137
     */
138
    public function __get($dbname)
139
    {
140
        return $this->selectDB($dbname);
141
    }
142
143
    /**
144
     * Gets the client for this object
145
     *
146
     * @internal This part is not of the ext-mongo API and should not be used
147
     * @return Client
148
     */
149
    public function getClient()
150
    {
151
        return $this->client;
152
    }
153
154
    /**
155
     * Get connections
156
     *
157
     * Returns an array of all open connections, and information about each of the servers
158
     *
159
     * @return array
160
     */
161
    static public function getConnections()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
162
    {
163
        return [];
164
    }
165
166
    /**
167
     * Get hosts
168
     *
169
     * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
170
     * set. Without a replica set, it will just return an array with one element containing the host that you are
171
     * connected to.
172
     *
173
     * @return array
174
     */
175
    public function getHosts()
176
    {
177
        return [];
178
    }
179
180
    /**
181
     * Kills a specific cursor on the server
182
     *
183
     * @link http://www.php.net/manual/en/mongoclient.killcursor.php
184
     * @param string $server_hash The server hash that has the cursor. This can be obtained through
185
     * {@link http://www.php.net/manual/en/mongocursor.info.php MongoCursor::info()}.
186
     * @param int|MongoInt64 $id The ID of the cursor to kill. You can either supply an {@link http://www.php.net/manual/en/language.types.integer.php int}
187
     * containing the 64 bit cursor ID, or an object of the
188
     * {@link http://www.php.net/manual/en/class.mongoint64.php MongoInt64} class. The latter is necessary on 32
189
     * bit platforms (and Windows).
190
     */
191
    public function killCursor($server_hash , $id)
0 ignored issues
show
Unused Code introduced by
The parameter $server_hash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
192
    {
193
194
    }
195
196
    /**
197
     * Lists all of the databases available
198
     *
199
     * @link http://php.net/manual/en/mongoclient.listdbs.php
200
     * @return array Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving the database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully.
201
     */
202
    public function listDBs()
203
    {
204
        return $this->client->listDatabases();
205
    }
206
207
    /**
208
     * Gets a database collection
209
     *
210
     * @link http://www.php.net/manual/en/mongoclient.selectcollection.php
211
     * @param string $db The database name.
212
     * @param string $collection The collection name.
213
     * @return MongoCollection Returns a new collection object.
214
     * @throws Exception Throws Exception if the database or collection name is invalid.
215
     */
216
    public function selectCollection($db, $collection)
217
    {
218
        return new MongoCollection($this->selectDB($db), $collection);
219
    }
220
221
    /**
222
     * Gets a database
223
     *
224
     * @link http://www.php.net/manual/en/mongo.selectdb.php
225
     * @param string $name The database name.
226
     * @return MongoDB Returns a new db object.
227
     * @throws InvalidArgumentException
228
     */
229
    public function selectDB($name)
230
    {
231
        return new MongoDB($this, $name);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function setReadPreference($readPreference, $tags = null)
238
    {
239
        return $this->setReadPreferenceFromParameters($readPreference, $tags);
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function setWriteConcern($wstring, $wtimeout = 0)
246
    {
247
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
248
    }
249
250
    /**
251
     * Choose a new secondary for slaveOkay reads
252
     *
253
     * @link www.php.net/manual/en/mongo.switchslave.php
254
     * @return string The address of the secondary this connection is using for reads. This may be the same as the previous address as addresses are randomly chosen. It may return only one address if only one secondary (or only the primary) is available.
255
     * @throws MongoException (error code 15) if it is called on a non-replica-set connection. It will also throw MongoExceptions if it cannot find anyone (primary or secondary) to read from (error code 16).
256
     */
257
    public function switchSlave()
258
    {
259
        return $this->server;
260
    }
261
262
    /**
263
     * String representation of this connection
264
     *
265
     * @link http://www.php.net/manual/en/mongoclient.tostring.php
266
     * @return string Returns hostname and port for this connection.
267
     */
268
    public function __toString()
269
    {
270
        return $this->server;
271
    }
272
}
273
274