DoctrineMongoDb   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 46
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 34 4
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
6
use Laminas\Diagnostics\Check\AbstractCheck;
7
use Laminas\Diagnostics\Result\Failure;
8
use Laminas\Diagnostics\Result\Success;
9
use MongoDB\Driver\Command;
10
use MongoDB\Driver\Exception\ConnectionException;
11
12
class DoctrineMongoDb extends AbstractCheck
13
{
14
    protected $manager;
15
    protected $connectionName;
16
17
    public function __construct(ManagerRegistry $registry, $connectionName = null)
18
    {
19
        $this->manager = $registry;
20
        $this->connectionName = $connectionName;
21
    }
22
23
    public function check()
24
    {
25
        $connection = $this->manager->getConnection($this->connectionName);
26
27
        // Using "mongo" PHP extension
28
        if (\method_exists($connection, 'connect')) {
29
            $connection->connect();
30
31
            if ($connection->isConnected()) {
32
                return new Success();
33
            }
34
35
            return new Failure(
36
                sprintf(
37
                    'Connection "%s" is unavailable.',
38
                    $this->connectionName
39
                )
40
            );
41
        }
42
43
        // Using "mongodb" PHP extension
44
        try {
45
            $connection->getManager()->executeCommand('test', new Command(['ping' => 1]));
46
        } catch (ConnectionException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\ConnectionException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47
            return new Failure(
48
                sprintf(
49
                    'Connection "%s" is unavailable.',
50
                    $this->connectionName
51
                )
52
            );
53
        }
54
55
        return new Success();
56
    }
57
}
58