MongoDriver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 20.69 %

Importance

Changes 0
Metric Value
dl 18
loc 87
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 11 4
A instance() 0 3 1
A setConfig() 17 17 3
A __construct() 0 8 2
A check() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the EventStoreManager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SimpleEventStoreManager\Infrastructure\Drivers;
12
13
use MongoDB\Client;
14
use SimpleEventStoreManager\Infrastructure\Drivers\Contracts\DriverInterface;
15
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\MalformedDriverConfigException;
16
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\ManageAggregateIndexException;
0 ignored issues
show
Bug introduced by
The type SimpleEventStoreManager\...AggregateIndexException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\NotInstalledDriverCheckException;
18
19
class MongoDriver implements DriverInterface
20
{
21
    /**
22
     * @var
23
     */
24
    private $config;
25
26
    /**
27
     * @var \Client
0 ignored issues
show
Bug introduced by
The type Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
     */
29
    private $instance;
30
31
    /**
32
     * PdoDriver constructor.
33
     *
34
     * @codeCoverageIgnore
35
     *
36
     * @param array $config
37
     *
38
     * @throws NotInstalledDriverCheckException
39
     */
40
    public function __construct(array $config = [])
41
    {
42
        $this->setConfig($config);
43
        if (!$this->check()) {
44
            throw new NotInstalledDriverCheckException('Pdo is not loaded.');
45
        }
46
47
        $this->connect();
48
    }
49
50
    /**
51
     * @param $config
52
     *
53
     * @throws MalformedDriverConfigException
54
     */
55 View Code Duplication
    private function setConfig($config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $allowedConfigKeys = [
58
            'database',
59
            'host',
60
            'password',
61
            'port',
62
            'username',
63
        ];
64
65
        foreach (array_keys($config) as $key) {
66
            if (!in_array($key, $allowedConfigKeys)) {
67
                throw new MalformedDriverConfigException('Mongo Driver: malformed config parameters');
68
            }
69
        }
70
71
        $this->config = $config;
72
    }
73
74
    /**
75
     * @codeCoverageIgnore
76
     *
77
     * @return bool
78
     */
79
    public function check()
80
    {
81
        return class_exists('\MongoDB\Client');
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    public function connect()
88
    {
89
        $connectionString = 'mongodb://';
90
        $connectionString .= (isset($this->config['username'])  && isset($this->config['password'])) ? $this->config['username'].':'.$this->config['password'].'@' : '';
91
        $connectionString .= $this->config['host'].':'.$this->config['port'];
92
93
        $database = (isset($this->config['database'])) ? $this->config['database'] : 'eventstore';
94
95
        $this->instance = (new Client($connectionString))->selectDatabase($database);
0 ignored issues
show
Documentation Bug introduced by
It seems like new MongoDB\Client($conn...lectDatabase($database) of type MongoDB\Database is incompatible with the declared type Client of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
96
97
        return true;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function instance()
104
    {
105
        return $this->instance;
106
    }
107
}
108