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.
Completed
Push — master ( 1e1b58...a9ac0a )
by De
02:31
created

Client::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
    private $dsn = self::DEFAULT_DSN;
27
    
28
    private $connectOptions = array();
29
    
30
    /**
31
     *
32
     * @var \MongoClient
33
     */
34
    private $mongoClient;
35
    
36
    private $databasePool = array();
37
    
38
    /**
39
     * @var array Database to class mapping
40
     */
41
    protected $_mapping = array();
42
    
43
        
44
    private $logger;
45
    
46
    private $currentDatabaseName;
47
48
    /**
49
     *
50
     * @var string version of MongoDb
51
     */
52
    private $dbVersion;
53
    
54
    private $debug = false;
55
56
    /**
57
     * 
58
     * @param string $dsn Data Source Name
59
     * @param array $options
60
     */
61
    public function __construct($dsn = null, array $options = null) {
62
        if($dsn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dsn of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
            $this->setDsn($dsn);
64
        }
65
        
66
        if($options) {
67
            $this->setConnectOptions($options);
68
        }
69
    }
70
    
71
    /**
72
     * Set credentials to auth on db, specified in connect options or dsn.
73
     * If not specified - auth on admin db
74
     * 
75
     * @param type $username
76
     * @param type $password
77
     * @return \Sokil\Mongo\Client
78
     */
79
    public function setCredentials($username, $password)
80
    {
81
        $this->connectOptions['username'] = $username;
82
        $this->connectOptions['password'] = $password;
83
        
84
        return $this;
85
    }
86
    
87
    public function __get($name)
88
    {
89
        return $this->getDatabase($name);
90
    }
91
    
92
    /**
93
     * 
94
     * @return string Version of PHP driver
95
     */
96
    public function getVersion()
97
    {
98
        return \MongoClient::VERSION;
99
    }
100
101
    /**
102
     *
103
     * @return string version of mongo database
104
     */
105
    public function getDbVersion()
106
    {
107
        if ($this->dbVersion) {
108
            return $this->dbVersion;
109
        }
110
111
        $buildInfo = $this
112
            ->getDatabase('admin')
113
            ->executeCommand(array('buildinfo' => 1));
114
        
115
        $this->dbVersion = $buildInfo['version'];
116
        return $this->dbVersion;
117
    }
118
    
119
    public function setDsn($dsn)
120
    {
121
        $this->dsn = $dsn;
122
        return $this;
123
    }
124
    
125
    public function getDsn()
126
    {
127
        return $this->dsn;
128
    }
129
    
130
    /**
131
     * Set connect options
132
     * 
133
     * @link http://php.net/manual/en/mongoclient.construct.php connect options
134
     * @param array $options
135
     * @return \Sokil\Mongo\Client
136
     */
137
    public function setConnectOptions(array $options)
138
    {
139
        $this->connectOptions = $options;
140
        return $this;
141
    }
142
143
    public function getConnectOptions()
144
    {
145
        return $this->connectOptions;
146
    }
147
    
148
    /**
149
     * Set mongo's client
150
     * 
151
     * @param \MongoClient $client
152
     * @return \Sokil\Mongo\Client
153
     */
154
    public function setMongoClient(\MongoClient $client)
155
    {
156
        $this->mongoClient = $client;
157
        return $this;
158
    }
159
    
160
    /**
161
     * Get mongo connection instance
162
     *
163
     * @return \MongoClient
164
     * @throws \Sokil\Mongo\Exception
165
     */
166
    public function getMongoClient()
167
    {
168
        if($this->mongoClient) {
169
            return $this->mongoClient;
170
        }
171
172
        $this->mongoClient = new \MongoClient($this->dsn, $this->connectOptions);
173
        
174
        return $this->mongoClient;
175
    }
176
    
177
    /**
178
     * Get list of all active connections through this client
179
     * 
180
     * @return type
181
     */
182
    public function getConnections()
183
    {
184
        return $this->mongoClient->getConnections();
185
    }
186
    
187
    /**
188
     * Map database and collection name to class
189
     * 
190
     * @param array $mapping classpath or class prefix
191
     * Classpath:
192
     *  [dbname => [collectionName => collectionClass, ...], ...]
193
     * Class prefix:
194
     *  [dbname => classPrefix]
195
     * 
196
     * @return \Sokil\Mongo\Client
197
     */
198
    public function map(array $mapping) {
199
        $this->_mapping = $mapping;
200
        
201
        return $this;
202
    }
203
    
204
    /**
205
     * 
206
     * @param string $name database name
207
     * @return \Sokil\Mongo\Database
208
     */
209
    public function getDatabase($name = null) {
210
        
211
        if(!$name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
212
            $name = $this->getCurrentDatabaseName();
213
        }
214
215
        if(!isset($this->databasePool[$name])) {
216
            // init db
217
            $database = new Database($this, $name);
218
            if(isset($this->_mapping[$name])) {
219
                $database->map($this->_mapping[$name]);
220
            }
221
222
            // configure db
223
            $this->databasePool[$name] = $database;
224
        }
225
        
226
        return $this->databasePool[$name];
227
    }
228
    
229
    /**
230
     * Select database
231
     * 
232
     * @param string $name
233
     * @return \Sokil\Mongo\Client
234
     */
235
    public function useDatabase($name)
236
    {
237
        $this->currentDatabaseName = $name;
238
        return $this;
239
    }
240
    
241
    public function getCurrentDatabaseName()
242
    {
243
        if(!$this->currentDatabaseName) {
244
            throw new Exception('Database not selected');
245
        }
246
247
        return $this->currentDatabaseName;
248
    }
249
    
250
    /**
251
     * Get collection from previously selected database by self::useDatabase()
252
     * 
253
     * @param string $name
254
     * @return \Sokil\Mongo\Collection
255
     * @throws Exception
256
     */
257
    public function getCollection($name)
258
    {        
259
        return $this
260
            ->getDatabase($this->getCurrentDatabaseName())
261
            ->getCollection($name);
262
    }
263
    
264
    public function readPrimaryOnly()
265
    {
266
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_PRIMARY);
267
        return $this;
268
    }
269
    
270
    public function readPrimaryPreferred(array $tags = null)
271
    {
272
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_PRIMARY_PREFERRED, $tags);
273
        return $this;
274
    }
275
    
276
    public function readSecondaryOnly(array $tags = null)
277
    {
278
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_SECONDARY, $tags);
279
        return $this;
280
    }
281
    
282
    public function readSecondaryPreferred(array $tags = null)
283
    {
284
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
285
        return $this;
286
    }
287
    
288
    public function readNearest(array $tags = null)
289
    {
290
        $this->getMongoClient()->setReadPreference(\MongoClient::RP_NEAREST, $tags);
291
        return $this;
292
    }
293
294
    public function getReadPreference()
295
    {
296
        return $this->getMongoClient()->getReadPreference();
297
    }
298
    
299
    public function setLogger(LoggerInterface $logger)
300
    {
301
        $this->logger = $logger;
302
        return $this;
303
    }
304
    
305
    /**
306
     * 
307
     * @return \Psr\Log\LoggerInterface
308
     */
309
    public function getLogger()
310
    {
311
        return $this->logger;
312
    }
313
314
    /**
315
     * Check if logger defined
316
     *
317
     * @return bool
318
     */
319
    public function hasLogger()
320
    {
321
        return (bool) $this->logger;
322
    }
323
324
    /**
325
     * Remove logger
326
     *
327
     * @return \Sokil\Mongo\Client
328
     */
329
    public function removeLogger()
330
    {
331
        $this->logger = null;
332
        return $this;
333
    }
334
335
    /**
336
     * Enable or disable debug mode
337
     */
338
    public function debug($enabled = true)
339
    {
340
        $this->debug = (bool) $enabled;
341
        return $this;
342
    }
343
344
    /**
345
     * Check state of debug mode
346
     */
347
    public function isDebugEnabled()
348
    {
349
        return $this->debug;
350
    }
351
352
    /**
353
     * Define write concern on whole requests
354
     *
355
     * @param string|integer $w write concern
356
     * @param int $timeout timeout in milliseconds
357
     * @return \Sokil\Mongo\Client
358
     *
359
     * @throws \Sokil\Mongo\Exception
360
     */
361
    public function setWriteConcern($w, $timeout = 10000)
362
    {
363
        if(!$this->getMongoClient()->setWriteConcern($w, (int) $timeout)) {
364
            throw new Exception('Error setting write concern');
365
        }
366
        
367
        return $this;
368
    }
369
    
370
    /**
371
     * Define unacknowledged write concern on whole requests
372
     *
373
     * @param int $timeout timeout in milliseconds
374
     * @return \Sokil\Mongo\Client
375
     */
376
    public function setUnacknowledgedWriteConcern($timeout = 10000)
377
    {
378
        $this->setWriteConcern(0, (int) $timeout);
379
        return $this;
380
    }
381
    
382
    /**
383
     * Define majority write concern on whole requests
384
     *
385
     * @param int $timeout timeout in milliseconds
386
     * @return \Sokil\Mongo\Client
387
     */
388
    public function setMajorityWriteConcern($timeout = 10000)
389
    {
390
        $this->setWriteConcern('majority', (int) $timeout);
391
        return $this;
392
    }
393
394
    /**
395
     * Get currently active write concern on connection level
396
     *
397
     * @return string|int
398
     */
399
    public function getWriteConcern()
400
    {
401
        return $this->getMongoClient()->getWriteConcern();
402
    }
403
404
    /**
405
     * Create new persistence manager
406
     * @return \Sokil\Mongo\Persistence
407
     */
408
    public function createPersistence()
409
    {
410
        // operations of same type and in same collection executed at once 
411
        if (version_compare($this->getVersion(), '1.5', '>=') && version_compare($this->getDbVersion(), '2.6', '>=')) {
412
            return new Persistence();
413
        }
414
415
        // all operations executed separately
416
        return new PersistenceLegacy();
417
    }
418
}
419