AuthenticatingMongoTemplate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 64.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 65
ccs 9
cts 14
cp 0.6429
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getDatabase() 0 4 1
A setLogger() 0 4 1
1
<?php
2
/**
3
 * This file is part of the SmartGecko(c) business platform.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Governor\Framework\Common\Mongo;
10
11
use Psr\Log\LoggerAwareInterface;
12
use Governor\Framework\Common\Logging\NullLogger;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * Abstract implementation for Mongo templates. Mongo templates give access to the collections in a Mongo Database used
17
 * by components of the Axon Framework. The AuthenticatingMongoTemplate takes care of the authentication against the
18
 * Mongo database.
19
 *
20
 * @author    "David Kalosi" <[email protected]>
21
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
22
 */
23
abstract class AuthenticatingMongoTemplate implements LoggerAwareInterface
24
{
25
26
    const DEFAULT_GOVERNOR_DATABASE = "governor";
27
28
    /**
29
     * @var
30
     */
31
    private $logger;
32
33
    /**
34
     * @var \MongoDB
35
     */
36
    private $database;
37
38
39
    /**
40
     * Initializes the MongoTemplate to connect to the database with given
41
     * <code>databaseName</code>. The given <code>userName</code> and <code>password</code>, when not
42
     * <code>null</code>, are used to authenticate against the database.
43
     *
44
     * @param string $server The MongoDB connect string
45
     * @param string $databaseName The name of the database
46
     * @param string $authenticationDatabaseName The name of the database containing the user to authenticate
47
     */
48 22
    public function __construct($server, $databaseName, $authenticationDatabaseName = null)
49
    {
50 22
        $this->logger = new NullLogger();
51 22
        $options = [];
52
53 22
        if (null !== $authenticationDatabaseName) {
54
            $options['authSource'] = $authenticationDatabaseName;
55
        }
56
57 22
        $client = new \MongoClient($server, $options);
58 22
        $this->database = $client->selectDB($databaseName);
59 22
    }
60
61
    /**
62
     * Returns a reference to the Database with the configured database name. If a username and/or password have been
63
     * provided, these are used to authenticate against the database.
64
     * <p/>
65
     * Note that the configured <code>userName</code> and <code>password</code> are ignored if the database is already
66
     * in an authenticated state.
67
     *
68
     * @return \MongoDB DB instance, referring to the database with configured name.
69
     */
70 22
    protected function getDatabase()
71
    {
72 22
        return $this->database;
73
    }
74
75
    /**
76
     * Sets a logger instance on the object
77
     *
78
     * @param LoggerInterface $logger
79
     * @return null
80
     */
81
    public function setLogger(LoggerInterface $logger)
82
    {
83
        $this->logger = $logger;
84
    }
85
86
87
}