GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Client::setWriteConcern()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo;
13
14
use \Psr\Log\LoggerInterface;
15
16
/**
17
 * Connection manager and factory to get database and collection instances.
18
 *
19
 * @link https://github.com/sokil/php-mongo#connecting Connecting
20
 * @link https://github.com/sokil/php-mongo#selecting-database-and-collection Get database and collection instance
21
 */
22
class Client
23
{
24
    const DEFAULT_DSN = 'mongodb://127.0.0.1';
25
26
    /**
27
     * @var string
28
     */
29
    private $dsn = self::DEFAULT_DSN;
30
31
    /**
32
     * @var array
33
     */
34
    private $connectOptions = array();
35
    
36
    /**
37
     *
38
     * @var \MongoClient
39
     */
40
    private $mongoClient;
41
42
    /**
43
     * @var array
44
     */
45
    private $databasePool = array();
46
    
47
    /**
48
     * @var array Database to class mapping
49
     */
50
    private $mapping = array();
51
52
    /**
53
     * @var LoggerInterface|null
54
     */
55
    private $logger;
56
57
    /**
58
     * @var string
59
     */
60
    private $currentDatabaseName;
61
62
    /**
63
     *
64
     * @var string version of MongoDb
65
     */
66
    private $dbVersion;
67
68
    /**
69
     * @var bool
70
     */
71
    private $debug = false;
72
73
    /**
74
     *
75
     * @param string $dsn Data Source Name
76
     * @param array $options
77
     */
78
    public function __construct($dsn = null, array $options = null)
79
    {
80
        if (!empty($dsn)) {
81
            $this->setDsn($dsn);
82
        }
83
        
84
        if (!empty($options)) {
85
            $this->setConnectOptions($options);
86
        }
87
    }
88
89
    /**
90
     * Check if client emulates ext-mongo driver by new ext-mongodb extension
91
     *
92
     * @return bool
93
     */
94
    public static function isEmulationMode()
95
    {
96
        return class_exists('\MongoDB\Driver\Manager');
97
    }
98
    
99
    /**
100
     * Set credentials to auth on db, specified in connect options or dsn.
101
     * If not specified - auth on admin db
102
     *
103
     * @param string $username
104
     * @param string $password
105
     * @return \Sokil\Mongo\Client
106
     */
107
    public function setCredentials($username, $password)
108
    {
109
        $this->connectOptions['username'] = $username;
110
        $this->connectOptions['password'] = $password;
111
        
112
        return $this;
113
    }
114
    
115
    public function __get($name)
116
    {
117
        return $this->getDatabase($name);
118
    }
119
    
120
    /**
121
     *
122
     * @return string Version of PHP driver
123
     */
124
    public function getVersion()
125
    {
126
        return \MongoClient::VERSION;
127
    }
128
129
    /**
130
     *
131
     * @return string version of mongo database
132
     */
133
    public function getDbVersion()
134
    {
135
        if ($this->dbVersion) {
136
            return $this->dbVersion;
137
        }
138
139
        $buildInfo = $this
140
            ->getDatabase('admin')
141
            ->executeCommand(array('buildinfo' => 1));
142
        
143
        $this->dbVersion = $buildInfo['version'];
144
        return $this->dbVersion;
145
    }
146
    
147
    public function setDsn($dsn)
148
    {
149
        $this->dsn = $dsn;
150
        return $this;
151
    }
152
    
153
    public function getDsn()
154
    {
155
        return $this->dsn;
156
    }
157
    
158
    /**
159
     * Set connect options
160
     *
161
     * @link http://php.net/manual/en/mongoclient.construct.php connect options
162
     * @param array $options
163
     * @return \Sokil\Mongo\Client
164
     */
165
    public function setConnectOptions(array $options)
166
    {
167
        $this->connectOptions = $options;
168
        return $this;
169
    }
170
171
    public function getConnectOptions()
172
    {
173
        return $this->connectOptions;
174
    }
175
    
176
    /**
177
     * Set mongo's client
178
     *
179
     * @param \MongoClient $client
180
     * @return \Sokil\Mongo\Client
181
     */
182
    public function setMongoClient(\MongoClient $client)
183
    {
184
        $this->mongoClient = $client;
185
        return $this;
186
    }
187
    
188
    /**
189
     * Get mongo connection instance
190
     *
191
     * @return \MongoClient
192
     * @throws Exception
193
     */
194
    public function getMongoClient()
195
    {
196
        if ($this->mongoClient) {
197
            return $this->mongoClient;
198
        }
199
200
        $this->mongoClient = new \MongoClient(
201
            $this->dsn,
202
            $this->connectOptions
203
        );
204
        
205
        return $this->mongoClient;
206
    }
207
    
208
    /**
209
     * Get list of all active connections through this client
210
     *
211
     * @return array
212
     */
213
    public function getConnections()
214
    {
215
        return $this->mongoClient->getConnections();
216
    }
217
    
218
    /**
219
     * Map database and collection name to class.
220
     *
221
     * Collection name -> array definition:
222
     *  ['acmeDatabaseName' => ['acmeCollectionName' => ['class' => '\Acme\Collection\SomeCollectionClass']]]
223
     * Collection name -> collection class name (deprecated: use definition array):
224
     *  ['acmeDatabaseName' => ['acmeCollectionName' => '\Acme\Collection\SomeCollectionClass']]
225
     * Collection's class namespace (deprecated: use definition array):
226
     *  ['acmeDatabaseName' => '\Acme\Collection']
227
     *
228
     * @param array $mapping classpath or class prefix
229
     * @return Client
230
     */
231
    public function map(array $mapping)
232
    {
233
        $this->mapping = $mapping;
234
        
235
        return $this;
236
    }
237
    
238
    /**
239
     * Get database instance
240
     *
241
     * @param string    $name   database name
242
     * @return Database
243
     */
244
    public function getDatabase($name = null)
245
    {
246
        if (empty($name)) {
247
            $name = $this->getCurrentDatabaseName();
248
        }
249
250
        if (!isset($this->databasePool[$name])) {
251
            // init db
252
            $database = new Database($this, $name);
253
            if (isset($this->mapping[$name])) {
254
                $database->map($this->mapping[$name]);
255
            }
256
257
            // configure db
258
            $this->databasePool[$name] = $database;
259
        }
260
        
261
        return $this->databasePool[$name];
262
    }
263
    
264
    /**
265
     * Select database
266
     *
267
     * @param string $name
268
     * @return \Sokil\Mongo\Client
269
     */
270
    public function useDatabase($name)
271
    {
272
        $this->currentDatabaseName = $name;
273
        return $this;
274
    }
275
276
    /**
277
     * Get name of current database
278
     *
279
     * @return string
280
     * @throws Exception
281
     */
282
    public function getCurrentDatabaseName()
283
    {
284
        if (!$this->currentDatabaseName) {
285
            throw new Exception('Database not selected');
286
        }
287
288
        return $this->currentDatabaseName;
289
    }
290
    
291
    /**
292
     * Get collection from previously selected database by self::useDatabase()
293
     *
294
     * @param string $name
295
     * @return \Sokil\Mongo\Collection
296
     * @throws Exception
297
     */
298
    public function getCollection($name)
299
    {
300
        return $this
301
            ->getDatabase($this->getCurrentDatabaseName())
302
            ->getCollection($name);
303
    }
304
    
305
    public function readPrimaryOnly()
306
    {
307
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_PRIMARY);
308
        return $this;
309
    }
310
    
311
    public function readPrimaryPreferred(array $tags = null)
312
    {
313
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_PRIMARY_PREFERRED, $tags);
314
        return $this;
315
    }
316
    
317
    public function readSecondaryOnly(array $tags = null)
318
    {
319
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_SECONDARY, $tags);
320
        return $this;
321
    }
322
    
323
    public function readSecondaryPreferred(array $tags = null)
324
    {
325
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
326
        return $this;
327
    }
328
    
329
    public function readNearest(array $tags = null)
330
    {
331
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_NEAREST, $tags);
332
        return $this;
333
    }
334
335
    public function getReadPreference()
336
    {
337
        return $this->getMongoClient()->getReadPreference();
338
    }
339
    
340
    public function setLogger(LoggerInterface $logger)
341
    {
342
        $this->logger = $logger;
343
        return $this;
344
    }
345
    
346
    /**
347
     *
348
     * @return \Psr\Log\LoggerInterface
349
     */
350
    public function getLogger()
351
    {
352
        return $this->logger;
353
    }
354
355
    /**
356
     * Check if logger defined
357
     *
358
     * @return bool
359
     */
360
    public function hasLogger()
361
    {
362
        return (bool) $this->logger;
363
    }
364
365
    /**
366
     * Remove logger
367
     *
368
     * @return \Sokil\Mongo\Client
369
     */
370
    public function removeLogger()
371
    {
372
        $this->logger = null;
373
        return $this;
374
    }
375
376
    /**
377
     * Enable or disable debug mode
378
     */
379
    public function debug($enabled = true)
380
    {
381
        $this->debug = (bool) $enabled;
382
        return $this;
383
    }
384
385
    /**
386
     * Check state of debug mode
387
     */
388
    public function isDebugEnabled()
389
    {
390
        return $this->debug;
391
    }
392
393
    /**
394
     * Define write concern on whole requests
395
     *
396
     * @param string|integer $w write concern
397
     * @param int $timeout timeout in milliseconds
398
     * @return \Sokil\Mongo\Client
399
     *
400
     * @throws \Sokil\Mongo\Exception
401
     */
402
    public function setWriteConcern($w, $timeout = 10000)
403
    {
404
        if (!$this->getMongoClient()->setWriteConcern($w, (int) $timeout)) {
405
            throw new Exception('Error setting write concern');
406
        }
407
        
408
        return $this;
409
    }
410
    
411
    /**
412
     * Define unacknowledged write concern on whole requests
413
     *
414
     * @param int $timeout timeout in milliseconds
415
     * @return \Sokil\Mongo\Client
416
     */
417
    public function setUnacknowledgedWriteConcern($timeout = 10000)
418
    {
419
        $this->setWriteConcern(0, (int) $timeout);
420
        return $this;
421
    }
422
    
423
    /**
424
     * Define majority write concern on whole requests
425
     *
426
     * @param int $timeout timeout in milliseconds
427
     * @return \Sokil\Mongo\Client
428
     */
429
    public function setMajorityWriteConcern($timeout = 10000)
430
    {
431
        $this->setWriteConcern('majority', (int) $timeout);
432
        return $this;
433
    }
434
435
    /**
436
     * Get currently active write concern on connection level
437
     *
438
     * @return string|int
439
     */
440
    public function getWriteConcern()
441
    {
442
        return $this->getMongoClient()->getWriteConcern();
443
    }
444
445
    /**
446
     * Create new persistence manager
447
     * @return \Sokil\Mongo\Persistence
448
     */
449
    public function createPersistence()
450
    {
451
        // operations of same type and in same collection executed at once
452
        if (version_compare($this->getVersion(), '1.5', '>=') && version_compare($this->getDbVersion(), '2.6', '>=')) {
453
            return new Persistence();
454
        }
455
456
        // all operations executed separately
457
        return new PersistenceLegacy();
458
    }
459
}
460