Completed
Push — master ( aa9cca...5f1e07 )
by Elan
18s queued 15s
created

MongoStorageProvider::register()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace XHGui\ServiceProvider;
4
5
use MongoClient;
6
use MongoCollection;
7
use MongoDB;
8
use MongoDB\Driver\Manager;
9
use Pimple\Container;
10
use Pimple\ServiceProviderInterface;
11
use RuntimeException;
12
use XHGui\Saver\MongoSaver;
13
use XHGui\Searcher\MongoSearcher;
14
15
class MongoStorageProvider implements ServiceProviderInterface
16
{
17
    public function register(Container $app): void
18
    {
19
        // NOTE: db.host, db.options, db.driverOptions, db.db are @deprecated and will be removed in the future
20
        $app['mongodb.database'] = static function ($app) {
21
            $config = $app['config'];
22
            $mongodb = $config['mongodb'] ?? [];
23
24
            return $config['db.db'] ?? $mongodb['database'] ?? 'xhgui';
25
        };
26
27
        $app[MongoDB::class] = static function ($app) {
28
            $database = $app['mongodb.database'];
29
            /** @var MongoClient $client */
30
            $client = $app[MongoClient::class];
31
            $mongoDB = $client->selectDb($database);
32
            $mongoDB->results->findOne();
33
34
            return $mongoDB;
35
        };
36
37
        $app[MongoClient::class] = static function ($app) {
38
            if (!class_exists(Manager::class)) {
39
                throw new RuntimeException('Required extension ext-mongodb missing');
40
            }
41
42
            $config = $app['config'];
43
            $mongodb = $config['mongodb'] ?? [];
44
            $options = $config['db.options'] ?? $mongodb['options'] ?? [];
45
            $driverOptions = $config['db.driverOptions'] ?? $mongodb['driverOptions'] ?? [];
46
            $server = $config['db.host'] ?? sprintf('mongodb://%s:%s', $mongodb['hostname'], $mongodb['port']);
47
48
            return new MongoClient($server, $options, $driverOptions);
49
        };
50
51
        $app['searcher.mongodb'] = static function ($app) {
52
            return new MongoSearcher($app[MongoDB::class]);
53
        };
54
55
        $app['saver.mongodb'] = static function ($app) {
56
            /** @var MongoDB $mongoDB */
57
            $mongoDB = $app[MongoDB::class];
58
            /** @var MongoCollection $collection */
59
            $collection = $mongoDB->results;
0 ignored issues
show
Bug introduced by
The property results does not seem to exist in MongoDB.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
61
            return new MongoSaver($collection);
62
        };
63
    }
64
}
65