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

MongoCollection::count()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 12
nc 8
nop 3
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
/**
12
 * MongoCollection implementation (somewhat) compatible with legacy mongo extension
13
 *
14
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
15
 */
16
class MongoCollection
17
{
18
19
	/**
20
	 * Creates a new collection
21
	 *
22
	 * @param MongoDB $db   - Parent database.
23
	 * @param string  $name -
24
	 *
25
	 * @return - Returns a new collection object.
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
	 */
27
	public function __construct(MongoDB $db, $name)
28
	{
29
		$this->db = $db;
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
		$this->name = $name;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
		$this->readPreference = $db->getReadPreference();
0 ignored issues
show
Bug introduced by
The property readPreference does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
		$this->fqn = $db->_getFullCollectionName($name);
0 ignored issues
show
Bug introduced by
The property fqn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
		$this->client = $db->_getClient();
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
	}
35
36
	/**
37
	 * Counts the number of documents in this collection
38
	 *
39
	 * @param array $query - Associative array or object with fields to
40
	 *   match.
41
	 * @param int $limit - Specifies an upper limit to the number returned.
42
	 * @param int $skip  - Specifies a number of results to skip before
43
	 *   starting the count.
44
	 *
45
	 * @return int - Returns the number of documents matching the query.
46
	 */
47
	public function count(array $query = [], $limit = 0, $skip = 0)
48
	{
49
		$cmd = [
50
			'count' => $this->name,
51
			'query' => $query
52
		];
53
54
		if ($limit)
55
		{
56
			$cmd['limit'] = $limit;
57
		}
58
59
		if ($skip)
60
		{
61
			$cmd['skip'] = $skip;
62
		}
63
64
		$result = $this->db->command($cmd);
65
66
		if (isset($result['ok']))
67
		{
68
			return (int) $result['n'];
69
		}
70
71
		return false;
72
	}
73
74
	/**
75
	 * Queries this collection, returning a for the result set
76
	 *
77
	 * @param array $query - The fields for which to search. MongoDB's
78
	 *   query language is quite extensive.
79
	 * @param array $fields - Fields of the results to return.
80
	 *
81
	 * @return MongoCursor - Returns a cursor for the search results.
82
	 */
83
	public function find(array $query = [], array $fields = [])
84
	{
85
		return new MongoCursor($this->client, $this->fqn, $query, $fields);
86
	}
87
88
	/**
89
	 * Queries this collection, returning a single element
90
	 *
91
	 * @param array $query - The fields for which to search. MongoDB's
92
	 *   query language is quite extensive.
93
	 * @param array $fields - Fields of the results to return.
94
	 *
95
	 * @return array - Returns record matching the search or NULL.
96
	 */
97
	public function findOne($query = [], array $fields = [])
98
	{
99
		$cursor = $this->find($query, $fields)->limit(1);
100
101
		return $cursor->getNext();
0 ignored issues
show
Bug introduced by
The method getNext() does not seem to exist on object<Maslosoft\Mangan\Bridge\MongoCursor>.

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...
102
	}
103
104
}
105