Completed
Push — ext-mongodb ( c0ec81 )
by Peter
03:34
created

MongoDB   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getReadPreference() 0 4 1
A _getClient() 0 4 1
A _getFullCollectionName() 0 4 1
A selectCollection() 0 9 2
A command() 0 16 3
A getGridFS() 0 4 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Bridge;
10
11
use MongoDB\Driver\Manager;
12
13
/**
14
 * MongoDB
15
 *
16
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
17
 */
18
class MongoDB
19
{
20
21
	const PROFILING_OFF = 0;
22
	const PROFILING_SLOW = 1;
23
	const PROFILING_ON = 2;
24
	const NAMESPACES_COLLECTION = 'system.namespaces';
25
	const INDEX_COLLECTION = 'system.indexes';
26
27
	/**
28
	 * @var int
29
	 */
30
	public $w = 1;
31
32
	/**
33
	 * @var int
34
	 */
35
	public $w_timeout = 10000;
36
37
	/**
38
	 * @var string
39
	 */
40
	private $name;
41
42
	/**
43
	 * @var MongoClient
44
	 */
45
	private $client;
46
47
	/**
48
	 *
49
	 * @var Manager
50
	 */
51
	private $manager;
52
53
	/**
54
	 * @var array
55
	 */
56
	private $collections = [];
57
58
	/**
59
	 * @var array
60
	 */
61
	private $readPreference;
62
63
	/**
64
	 * Creates a new database
65
	 *
66
	 * @param MongoClient $client - Database connection.
67
	 * @param string $name - Database name.
68
	 */
69
	public function __construct(MongoClient $client, $name)
70
	{
71
		$this->name = $name;
72
		$this->client = $client;
73
		$this->manager = new Manager();
74
		$this->readPreference = $client->getReadPreference();
75
	}
76
77
	/**
78
	 * Get the read preference for this database
79
	 *
80
	 * @return array
81
	 */
82
	public function getReadPreference()
83
	{
84
		return $this->readPreference;
85
	}
86
87
	/**
88
	 * @return MongoClient
89
	 */
90
	public function _getClient()
91
	{
92
		return $this->client;
93
	}
94
95
	public function _getFullCollectionName($collectionName)
96
	{
97
		return $this->name . '.' . $collectionName;
98
	}
99
100
	/**
101
	 * Gets a collection
102
	 *
103
	 * @param string $name - The collection name.
104
	 *
105
	 * @return MongoCollection - Returns a new collection object.
106
	 */
107
	public function selectCollection($name)
108
	{
109
		if (!isset($this->collections[$name]))
110
		{
111
			$this->collections[$name] = new MongoCollection($this, $name);
112
		}
113
114
		return $this->collections[$name];
115
	}
116
117
	/**
118
	 * Execute a database command
119
	 *
120
	 * @param array $command - The query to send.
0 ignored issues
show
Bug introduced by
There is no parameter named $command. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
121
	 * @param array $options - This parameter is an associative array of
122
	 *   the form array("optionname" => boolean, ...).
123
	 *
124
	 * @return array - Returns database response.
125
	 */
126
	public function command(array $cmd, array $options = [])
127
	{
128
		$timeout = MongoCursor::$timeout;
129
		if (!empty($options['timeout']))
130
		{
131
			$timeout = $options['timeout'];
132
		}
133
134
		$protocol = empty($options['protocol']) ? $this->client->_getWriteProtocol() : $options['protocol'];
0 ignored issues
show
Bug introduced by
The method _getWriteProtocol() does not seem to exist on object<Maslosoft\Mangan\Bridge\MongoClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
136
		$response = $protocol->opQuery(
137
				"{$this->name}.\$cmd", $cmd, 0, -1, 0, $timeout
138
		);
139
140
		return $response['result'][0];
141
	}
142
143
	/**
144
	 * Fetches toolkit for dealing with files stored in this database
145
	 *
146
	 * @param string $prefix - The prefix for the files and chunks
147
	 *   collections.
148
	 *
149
	 * @return MongoGridFS - Returns a new gridfs object for this database.
150
	 */
151
	public function getGridFS($prefix = 'fs')
152
	{
153
		return new MongoGridFS($this, $prefix);
154
	}
155
156
}
157