Completed
Push — master ( 406a77...cc9072 )
by Bret R.
05:55
created

MongoDbCheck::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
use MongoDB\Client;
4
5
use BretRZaun\StatusPage\Result;
6
7
class MongoDbCheck extends AbstractCheck
8
{
9
    private $client;
10
    private $databases = [];
11
    private $collections = [];
12
13
    public function __construct(string $label, Client $client)
14
    {
15
        parent::__construct($label);
16
        $this->client = $client;
17
    }
18
19
    public function ensureDatabaseExists(string $database)
20
    {
21
        $this->databases[] = $database;
22
    }
23
24
    public function ensureDatabaseHasCollecion(string $database, string $collection)
25
    {
26
        $this->ensureDatabaseExists($database);
27
        if (!isset($this->collections[$database])) {
28
            $this->collections[$database] = [];
29
        }
30
        $this->collections[$database][] = $collection;
31
    }
32
33
    public function checkStatus(): Result
34
    {
35
        $result = new Result($this->label);
36
        try {
37
            $dbs = $this->client->listDatabaseNames()->getArrayCopy();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Iterator as the method getArrayCopy() does only exist in the following implementations of said interface: ArrayIterator, RecursiveArrayIterator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
38
39
            if (count($this->databases) > 0) {
40
                $this->checkDatabases($dbs);
41
            }
42
            if (count($this->collections) > 0) {
43
                $this->checkCollections($dbs);
44
            }
45
        } catch(\Exception $e) {
46
            $result->setError($e->getMessage());
47
        }
48
        return $result;
49
    }
50
51
    private function checkDatabases($databases)
52
    {
53
        foreach($this->databases as $database) {
54
            if (!in_array($database, $databases)) {
55
                throw new \RuntimeException('Database '.$database.' does not exist');
56
            }
57
        }
58
    }
59
60
    private function checkCollections(array $databases)
0 ignored issues
show
Unused Code introduced by
The parameter $databases 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...
61
    {
62
        foreach($this->collections as $databaseName => $collections) {
63
            $database = $this->client->selectDatabase($databaseName);
64
            $collectionNames = $database->listCollectionNames()->getArrayCopy();
65
            foreach($collections as $collection) {
66
                if (!in_array($collection, $collectionNames)) {
67
                    throw new \RuntimeException('Collection '.$collection.' does not exist in database '.$databaseName);
68
                }
69
            }
70
        }
71
    }
72
73
}