Passed
Push — master ( a560c8...7c10fa )
by Koldo
09:11
created

ContextFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

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 11
cts 12
cp 0.9167
crap 3.0052
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 2
    public function __invoke(
23
        ContainerInterface $container,
24
        string $contextName = ConfigProvider::DEFAULT_CONTEXT
25
    ): Context {
26 2
        $contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
27
28 2
        $contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY];
29 2
        if (self::NULL === $contextType) {
30 1
            return new NullContext();
31
        }
32
33 1
        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
        throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType));
43
    }
44
}
45