Completed
Push — master ( 71cc85...b00a6a )
by Alexey
37:06
created

MongoDriver::setCredentials()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace SfCod\QueueBundle\Service;
4
5
use MongoDB\Client;
6
use MongoDB\Database;
7
8
/**
9
 * Class MongoDriver
10
 *
11
 * @author Alexey Orlov <[email protected]>
12
 * @author Virchenko Maksim <[email protected]>
13
 *
14
 * @package SfCod\QueueBundle\Service
15
 */
16
class MongoDriver implements MongoDriverInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $dbname;
22
23
    /**
24
     * @var array
25
     */
26
    protected $credentials;
27
28
    /**
29
     * @var Client
30
     */
31
    protected $client;
32
33
    /**
34
     * Mongodb credentials
35
     *
36
     * @param string $uri MongoDB connection string
37
     * @param array $uriOptions Additional connection string options
38
     * @param array $driverOptions Driver-specific options
39
     */
40
    public function setCredentials(string $uri, array $uriOptions = [], array $driverOptions = [])
41
    {
42
        $this->credentials = [
43
            'uri' => $uri,
44
            'uriOptions' => $uriOptions,
45
            'driverOptions' => $driverOptions,
46
        ];
47
    }
48
49
    /**
50
     * Get mongodb client
51
     *
52
     * @return Client
53
     */
54
    public function getClient(): Client
55
    {
56
        if (null === $this->client) {
57
            $this->client = new Client(
58
                $this->credentials['uri'],
59
                $this->credentials['uriOptions'],
60
                $this->credentials['driverOptions']
61
            );
62
        }
63
64
        return $this->client;
65
    }
66
67
    /**
68
     * Set client
69
     *
70
     * @param MongoDB\Client $client
71
     */
72
    public function setClient(Client $client)
73
    {
74
        $this->client = $client;
75
    }
76
77
    /**
78
     * Set mongo dbname
79
     *
80
     * @param string $dbname
81
     */
82
    public function setDbname(string $dbname = 'db')
83
    {
84
        $this->dbname = $dbname;
85
    }
86
87
    /**
88
     * Get mongo database
89
     *
90
     * @param null|string $name
91
     *
92
     * @return Database
93
     */
94
    public function getDatabase(?string $name = null): Database
95
    {
96
        return $this->getClient()->selectDatabase($name ?? $this->dbname);
97
    }
98
}
99