MongoCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getManager() 0 5 1
A getDatabase() 0 5 1
A count() 0 9 1
A saveAll() 0 11 2
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