MongoCollection::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace cvweiss\projectbase;
4
5
use MongoDB\Driver\BulkWrite;
6
7
class MongoCollection
8
{
9
    private $collection = null;
10
    private $mongo = null;
11
12
    public function __construct($collection, $mongo = null)
13
    {
14
        $this->collection = $collection;
15
        $this->mongo = $mongo;
16
    }
17
18
    protected function getManager()
19
    {
20
        $this->mongo = $this->mongo ?? Mongo::get();
21
        return $this->mongo->getManager();
22
    }
23
24
    protected function getDatabase()
25
    {   
26
        $this->mongo = $this->mongo ?? Mongo::get();
27
        return $this->mongo->getDatabase();
28
    }
29
30
    public function count($query = [])
0 ignored issues
show
Unused Code introduced by
The parameter $query 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...
31
    {
32
        $command = new \MongoDB\Driver\Command(['count' => $this->collection]);
33
34
        $cursor = $this->getManager()->executeCommand($this->getDatabase(), $command);
35
        $results = $cursor->toArray()[0];
36
37
        return (int) $results->n ?? 0;
38
    }
39
40
    public function saveAll(array $docs):bool
41
    {
42
        $bulk = new BulkWrite(['ordered' => true]);
43
44
        foreach ($docs as $doc) {
45
            $doc->save($bulk);
46
        }
47
48
        $return = Mongo::get()->executeBulkWrite($this->collection, $bulk);
49
        return  (count($return->getWriteErrors()) == 0);
50
    }
51
}
52