MongoAdapterFactory::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\PSR11PhpCache\Adapter;
6
7
use Cache\Adapter\MongoDB\MongoDBCachePool;
8
use MongoDB\Collection;
9
use MongoDB\Driver\Manager;
10
use Psr\Container\ContainerInterface;
11
use WShafer\PSR11PhpCache\Exception\InvalidConfigException;
12
use WShafer\PSR11PhpCache\Exception\MissingExtensionException;
13
use WShafer\PSR11PhpCache\Exception\MissingLibraryException;
14
15
class MongoAdapterFactory implements FactoryInterface
16
{
17
    /**
18
     * MongoAdapterFactory constructor.
19
     *
20
     * @codeCoverageIgnore
21
     */
22
    public function __construct()
23
    {
24
        if (!extension_loaded('mongodb')) {
25
            throw new MissingExtensionException(
26
                'mongodb extension not installed.'
27
            );
28
        }
29
30
        if (!class_exists(Collection::class)) {
31
            throw new MissingLibraryException(
32
                'composer package mongodb/mongodb not installed.'
33
            );
34
        }
35
    }
36
37
    /**
38
     * @param ContainerInterface $container
39
     * @param array              $options
40
     *
41
     * @return MongoDBCachePool
42
     */
43 5
    public function __invoke(ContainerInterface $container, array $options): MongoDBCachePool
44
    {
45 5
        $instance = $this->getMongoInstance($container, $options);
46 2
        return new MongoDBCachePool($instance);
47
    }
48
49
    /**
50
     * @param ContainerInterface $container
51
     * @param array              $options
52
     * @return \MongoDB\Collection
53
     */
54 5
    protected function getMongoInstance(ContainerInterface $container, array $options): Collection
55
    {
56
        if (
57 5
            empty($options['service'])
58 5
            && empty($options['dsn'])
59
        ) {
60 1
            throw new InvalidConfigException(
61 1
                'You must provide either a container service or a connection string to use'
62
            );
63
        }
64
65 4
        if (!empty($options['service'])) {
66 1
            return $this->getInstanceFromContainer($container, $options['service']);
67
        }
68
69 3
        return $this->getInstanceFromConfig($options);
70
    }
71
72 1
    protected function getInstanceFromContainer(ContainerInterface $container, $name)
73
    {
74 1
        return $container->get($name);
75
    }
76
77
    /**
78
     * @param array $options
79
     *
80
     * @return Collection
81
     *
82
     * @SuppressWarnings(PHPMD.StaticAccess)
83
     */
84 3
    protected function getInstanceFromConfig(array $options): Collection
85
    {
86 3
        $dsn = $options['dsn'] ?? null;
87 3
        $databaseName = $options['database'] ?? null;
88 3
        $collectionName = $options['collection'] ?? null;
89
90 3
        if (empty($databaseName)) {
91 1
            throw new InvalidConfigException(
92 1
                'You must provide a database name to use'
93
            );
94
        }
95
96 2
        if (empty($collectionName)) {
97 1
            throw new InvalidConfigException(
98 1
                'You must provide a collection name to use'
99
            );
100
        }
101
102 1
        $manager = new Manager($dsn);
103 1
        return MongoDBCachePool::createCollection($manager, $databaseName, $collectionName);
104
    }
105
}
106