CollectionManager   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getCollection() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\MongoRepo;
5
6
use MongoDB\Client;
7
use MongoDB\Collection;
8
use MongoDB\Database;
9
10
class CollectionManager
11
{
12
13
    protected Database $db;
14
    /** @var Collection[] */
15
    protected array $store = [];
16
17 7
    public function __construct(array $params)
18
    {
19 7
        $client = new Client(
20 7
            $params['dsn'] ?? 'mongodb://127.0.0.1/',
21 7
            $params['uriOptions'] ?? [],
22 7
            $params['driverOptions'] ?? []
23
        );
24
25 7
        $this->db = $client->selectDatabase($params['db']);
26 7
    }
27
28 7
    public function getCollection(string $name): Collection
29
    {
30 7
        if (!array_key_exists($name, $this->store)) {
31 7
            $this->store[$name] = $this->db->selectCollection($name);
32
        }
33
34 7
        return $this->store[$name];
35
    }
36
}
37