Completed
Pull Request — master (#13)
by Andreas
02:40
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
    const VERSION = '1.6.12';
30
    const DEFAULT_HOST = "localhost" ;
31
    const DEFAULT_PORT = 27017 ;
32
    const RP_PRIMARY = "primary" ;
33
    const RP_PRIMARY_PREFERRED = "primaryPreferred" ;
34
    const RP_SECONDARY = "secondary" ;
35
    const RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
36
    const RP_NEAREST = "nearest" ;
37
38
    /**
39
     * @var bool
40
     * @deprecated This will not properly work as the underlying driver connects lazily
41
     */
42
    public $connected = false;
43
44
    /**
45
     * @var
46
     */
47
    public $status;
48
49
    /**
50
     * @var string
51
     */
52
    protected $server;
53
54
    /**
55
     * @var
56
     */
57
    protected $persistent;
58
59
    /**
60
     * @var Client
61
     */
62
    private $client;
63
64
    /**
65
     * Creates a new database connection object
66
     *
67
     * @link http://php.net/manual/en/mongo.construct.php
68
     * @param string $server The server name.
69
     * @param array $options An array of options for the connection.
70
     * @param array $driverOptions An array of options for the MongoDB driver.
71
     * @throws MongoConnectionException
72
     */
73
    public function __construct($server = 'default', array $options = ["connect" => true], array $driverOptions = [])
74
    {
75
        if ($server === 'default') {
76
            $server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT;
77
        }
78
79
        $this->server = $server;
80
        $this->client = new Client($server, $options, $driverOptions);
81
82
        if (isset($options['connect']) && $options['connect']) {
83
            $this->connect();
84
        }
85
    }
86
87
    /**
88
     * Closes this database connection
89
     *
90
     * @link http://www.php.net/manual/en/mongoclient.close.php
91
     * @param  boolean|string $connection
92
     * @return boolean If the connection was successfully closed.
93
     */
94
    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...
95
    {
96
        $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...
97
98
        return false;
99
    }
100
101
    /**
102
     * Connects to a database server
103
     *
104
     * @link http://www.php.net/manual/en/mongoclient.connect.php
105
     *
106
     * @throws MongoConnectionException
107
     * @return boolean If the connection was successful.
108
     */
109
    public function connect()
110
    {
111
        $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...
112
113
        return true;
114
    }
115
116
    /**
117
     * Drops a database
118
     *
119
     * @link http://www.php.net/manual/en/mongoclient.dropdb.php
120
     * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database.
121
     * @return array The database response.
122
     * @deprecated Use MongoDB::drop() instead.
123
     */
124
    public function dropDB($db)
125
    {
126
        return $this->selectDB($db)->drop();
127
    }
128
129
    /**
130
     * Gets a database
131
     *
132
     * @link http://php.net/manual/en/mongoclient.get.php
133
     * @param string $dbname The database name.
134
     * @return MongoDB The database name.
135
     */
136
    public function __get($dbname)
137
    {
138
        return $this->selectDB($dbname);
139
    }
140
141
    /**
142
     * Gets the client for this object
143
     *
144
     * @internal This part is not of the ext-mongo API and should not be used
145
     * @return Client
146
     */
147
    public function getClient()
148
    {
149
        return $this->client;
150
    }
151
152
    /**
153
     * Get connections
154
     *
155
     * Returns an array of all open connections, and information about each of the servers
156
     *
157
     * @return array
158
     */
159
    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...
160
    {
161
        return [];
162
    }
163
164
    /**
165
     * Get hosts
166
     *
167
     * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
168
     * set. Without a replica set, it will just return an array with one element containing the host that you are
169
     * connected to.
170
     *
171
     * @return array
172
     */
173
    public function getHosts()
174
    {
175
        return [];
176
    }
177
178
    /**
179
     * Kills a specific cursor on the server
180
     *
181
     * @link http://www.php.net/manual/en/mongoclient.killcursor.php
182
     * @param string $server_hash The server hash that has the cursor. This can be obtained through
183
     * {@link http://www.php.net/manual/en/mongocursor.info.php MongoCursor::info()}.
184
     * @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}
185
     * containing the 64 bit cursor ID, or an object of the
186
     * {@link http://www.php.net/manual/en/class.mongoint64.php MongoInt64} class. The latter is necessary on 32
187
     * bit platforms (and Windows).
188
     */
189
    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...
190
    {
191
192
    }
193
194
    /**
195
     * Lists all of the databases available
196
     *
197
     * @link http://php.net/manual/en/mongoclient.listdbs.php
198
     * @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.
199
     */
200
    public function listDBs()
201
    {
202
        return $this->client->listDatabases();
203
    }
204
205
    /**
206
     * Gets a database collection
207
     *
208
     * @link http://www.php.net/manual/en/mongoclient.selectcollection.php
209
     * @param string $db The database name.
210
     * @param string $collection The collection name.
211
     * @return MongoCollection Returns a new collection object.
212
     * @throws Exception Throws Exception if the database or collection name is invalid.
213
     */
214
    public function selectCollection($db, $collection)
215
    {
216
        return new MongoCollection($this->selectDB($db), $collection);
217
    }
218
219
    /**
220
     * Gets a database
221
     *
222
     * @link http://www.php.net/manual/en/mongo.selectdb.php
223
     * @param string $name The database name.
224
     * @return MongoDB Returns a new db object.
225
     * @throws InvalidArgumentException
226
     */
227
    public function selectDB($name)
228
    {
229
        return new MongoDB($this, $name);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function setReadPreference($readPreference, $tags = null)
236
    {
237
        return $this->setReadPreferenceFromParameters($readPreference, $tags);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function setWriteConcern($wstring, $wtimeout = 0)
244
    {
245
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
246
    }
247
248
    /**
249
     * Choose a new secondary for slaveOkay reads
250
     *
251
     * @link www.php.net/manual/en/mongo.switchslave.php
252
     * @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.
253
     * @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).
254
     */
255
    public function switchSlave()
256
    {
257
        return $this->server;
258
    }
259
260
    /**
261
     * String representation of this connection
262
     *
263
     * @link http://www.php.net/manual/en/mongoclient.tostring.php
264
     * @return string Returns hostname and port for this connection.
265
     */
266
    public function __toString()
267
    {
268
        return $this->server;
269
    }
270
}
271
272