CollectionManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
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