Passed
Push — master ( a7c263...af90a5 )
by Koldo
02:34
created

ContextFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 3
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Queue\Container;
6
7
use Antidot\Queue\Container\Config\ConfigProvider;
8
use Assert\Assertion;
9
use Enqueue\Fs\FsConnectionFactory;
10
use Enqueue\Null\NullContext;
11
use Interop\Queue\Context;
12
use InvalidArgumentException;
13
use Psr\Container\ContainerInterface;
14
15
use function sprintf;
16
17
class ContextFactory
18
{
19
    private const NULL = 'null';
20
    private const FILESYSTEM = 'fs';
21
22 3
    public function __invoke(
23
        ContainerInterface $container,
24
        string $contextName = ConfigProvider::DEFAULT_CONTEXT
25
    ): Context {
26 3
        $contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
27
28 3
        $contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY];
29 3
        if (self::NULL === $contextType) {
30 1
            return new NullContext();
31
        }
32
33 2
        if (self::FILESYSTEM === $contextType) {
34 1
            Assertion::classExists(
35 1
                FsConnectionFactory::class,
36 1
                'Install "enqueue/fs" package to run filesystem context.'
37
            );
38 1
            Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.');
39 1
            return (new FsConnectionFactory($contextConfig['path']))->createContext();
40
        }
41
42 1
        throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType));
43
    }
44
}
45