Completed
Pull Request — develop (#575)
by
unknown
102:40 queued 37:52
created

DatabaseManager::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Graviton\ApiBundle\Manager;
4
5
6
class DatabaseManager
7
{
8
    /** @var string */
9
    protected $databseUri;
10
11
    /** @var string */
12
    protected $databaseName;
13
14
    /** @var \MongoClient */
15
    protected $dbClient;
16
17
    public function __construct(
18
        $dbUrl,
19
        $dbName
20
    ) {
21
        $this->databseUri = $dbUrl;
22
        $this->databaseName = $dbName;
23
    }
24
25
    /**
26
     * Connect to DB
27
     *
28
     * @throws \MongoConnectionException
29
     * @return \MongoClient
30
     */
31
    private function getClient()
32
    {
33
        if (!$this->dbClient) {
34
            $this->dbClient = new \MongoClient($this->databseUri, ['connect' => false]);
35
            $this->dbClient->connect();
36
        }
37
        return $this->dbClient;
38
    }
39
40
    private function find($collection, $filter = [], $sort = [], $limit = 1, $offset = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $sort 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...
Unused Code introduced by
The parameter $limit 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...
Unused Code introduced by
The parameter $offset 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...
41
    {
42
        /** @var \MongoCollection $collection */
43
        $collection = $this->getClient()->{$this->databaseName}->{$collection};
44
45
        $options = [];//['sort' => ['catid' => 1], 'limit' => 10];
46
        /** @var \MongoCursor $cursor */
47
        $cursor = $collection->find($filter, $options);
48
        return iterator_to_array($cursor, false);
49
    }
50
51
    public function findOne($collection, $id)
52
    {
53
        $filter = ['_id' => (string) $id];
54
        $data = $this->find($collection, $filter);
55
        if ($data) {
56
            return $data[0];
57
        }
58
        return [];
59
    }
60
61
    public function findAll($collection)
62
    {
63
        $data = $this->find($collection, [], [], 10);
64
        return $data;
65
    }
66
}
67