Completed
Pull Request — develop (#533)
by Mathias
07:53
created

MongoQueueFactory::__invoke()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Queue;
12
13
use Interop\Container\ContainerInterface;
14
use SlmQueue\Job\JobPluginManager;
15
use Zend\ServiceManager\Factory\FactoryInterface;
16
17
/**
18
 * Factory for \Core\Queue\MongoQueue
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 */
22
class MongoQueueFactory implements FactoryInterface
23
{
24
    
25 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
26
    {
27
        /* @var \Doctrine\ODM\MongoDB\DocumentManager $dm */
28 2
        $config = $container->get('config');
29 2
        $config = $config['slm_queue']['queues'][$requestedName] ?? [];
30 2
        $dm = $container->get('Core/DocumentManager');
31
32
        // @codeCoverageIgnoreStart
33
        if (isset($config['dns'])) {
34
            $client = new \MongoDB\Client($config['dns']);
35
        } else {
36
            $client = $dm->getConnection()->getMongoClient()->getClient();
37
        }
38
        // @codeCoverageIgnoreEnd
39
40 2
        if (!isset($config['db'])) {
41 1
            $config['db'] = $dm->getConfiguration()->getDefaultDB();
42
        }
43
44 2
        if (!isset($config['collection'])) {
45 1
            $config['collection'] = 'core.queue';
46
        }
47
48 2
        $collection = $client->selectCollection($config['db'], $config['collection']);
49 2
        $jobPlugins = $container->get(JobPluginManager::class);
50
51 2
        $service = new MongoQueue($collection, $requestedName, $jobPlugins);
52
        
53 2
        return $service;    
54
    }
55
}
56