Connection::isConnected()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver\MongoDB;
4
5
use MongoDB;
6
use Igni\Storage\Driver\Connection as ConnectionInterface;
7
use Igni\Storage\Exception\DriverException;
8
9
final class Connection implements ConnectionInterface
10
{
11
    /** @var array */
12
    private const VALID_FIND_OPTIONS = [
13
        'sort', 'projection', 'skip', 'limit', 'max', 'min'
14
    ];
15
16
    /** @var MongoDB\Driver\Manager */
17
    private $handler;
18
19
    /** @var string */
20
    private $host;
21
22
    /** @var ConnectionOptions */
23
    private $options;
24
25 49
    public function __construct(string $host, ConnectionOptions $options = null)
26
    {
27 49
        $this->host = $host;
28 49
        $this->options = $options;
29 49
    }
30
31 48
    public function close(): void
32
    {
33 48
        $this->handler = null;
34 48
    }
35
36 49
    public function connect(): void
37
    {
38 49
        $this->handler = new MongoDB\Driver\Manager(
39 49
            'mongodb://' . $this->host . '/' . $this->options->getDatabase(),
40 49
            $this->options->getURIOptions(),
41 49
            $this->options->getDriverOptions()
42
        );
43 49
    }
44
45 49
    public function isConnected(): bool
46
    {
47 49
        return $this->handler !== null;
48
    }
49
50 49
    public function createCursor(...$parameters): Cursor
51
    {
52 49
        if (!$this->isConnected()) {
53 49
            $this->connect();
54
        }
55
56 49
        $command = new MongoDB\Driver\Command($parameters[0]);
57
58 49
        return new Cursor($this, $this->options, $command);
59
    }
60
61 49
    public function dropCollection(string $collection): void
62
    {
63 49
        $cursor = $this->createCursor([
64 49
            'drop' => $collection,
65
        ]);
66 49
        $cursor->execute();
67 48
    }
68
69 49
    public function insert(string $collection, array ...$documents): void
70
    {
71 49
        $cursor = $this->createCursor([
72 49
            'insert' => $collection,
73 49
            'documents' => $documents,
74
        ]);
75 49
        $cursor->execute();
76 49
    }
77
78 2
    public function remove(string $collection, ...$ids): void
79
    {
80 2
        $deletes = [];
81 2
        foreach ($ids as $id) {
82 2
            $deletes[] = [
83
                'q' => [
84 2
                    '_id' => $id,
85
                ],
86 2
                'limit' => 1,
87
            ];
88
        }
89 2
        $cursor = $this->createCursor([
90 2
            'delete' => $collection,
91 2
            'deletes' => $deletes,
92
        ]);
93 2
        $cursor->execute();
94 2
    }
95
96 7
    public function find(string $collection, array $query = [], array $options = []): Cursor
97
    {
98 7
        if (!empty($options) && array_diff(array_keys($options), self::VALID_FIND_OPTIONS)) {
99
            throw DriverException::forOperationFailure('Invalid option passed to find query.');
100
        }
101 7
        $command = array_merge([
102 7
            'find' => $collection,
103 7
            'filter' => $query,
104 7
        ], $options);
105 7
        if (empty($command['filter'])) {
106 3
            unset ($command['filter']);
107
        }
108
109 7
        return $this->createCursor($command);
110
    }
111
112
    public function count(string $collection, array $query): Cursor
113
    {
114
        return $this->createCursor([
115
            'count' => $collection,
116
            'query' => $query,
117
        ]);
118
    }
119
120 1
    public function update(string $collection, array ...$documents): void
121
    {
122 1
        $updates = [];
123 1
        foreach ($documents as $document) {
124 1
            $id = null;
125 1
            if (!isset($document['_id'])) {
126 1
                if (!isset($document['id'])) {
127
                    throw DriverException::forOperationFailure('Cannot update documents without identity.');
128
                }
129 1
                $id = $document['id'];
130 1
                unset($document['id']);
131
            } else {
132
                $id = $document['_id'];
133
                unset($document['_id']);
134
            }
135
136 1
            $updates[] = [
137 1
                'q' => [
138 1
                    '_id' => $id,
139
                ],
140 1
                'u' => $document,
141
                'upsert' => true,
142
            ];
143
        }
144 1
        $cursor = $this->createCursor([
145 1
            'update' => $collection,
146 1
            'updates' => $updates,
147
        ]);
148 1
        $cursor->execute();
149 1
    }
150
151 49
    public function getBaseConnection(): MongoDB\Driver\Manager
152
    {
153 49
        return $this->handler;
154
    }
155
}
156