Passed
Push — master ( 3fec98...825426 )
by Koldo
02:42
created

ConfigProvider::getContextConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Queue\Container\Config;
6
7
use Antidot\Queue\Cli\StartQueueConsumer;
0 ignored issues
show
Bug introduced by
The type Antidot\Queue\Cli\StartQueueConsumer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Antidot\Queue\Container\ActionContainerFactory;
0 ignored issues
show
Bug introduced by
The type Antidot\Queue\Container\ActionContainerFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Antidot\Queue\Container\MessageProcessorFactory;
10
use Antidot\Queue\Container\ProducerFactory;
11
use Antidot\Queue\Container\StartQueueConsumerFactory;
12
use Antidot\Queue\MessageProcessor;
13
use Antidot\Queue\Producer;
14
use Assert\Assertion;
15
use Assert\AssertionFailedException;
16
use Enqueue\Consumption\QueueConsumer;
17
use Enqueue\Consumption\QueueConsumerInterface;
18
use Enqueue\Null\NullContext;
19
20
use InvalidArgumentException;
21
22
use function sprintf;
23
24
class ConfigProvider
25
{
26
    public const CONFIG_KEY = 'config';
27
    public const QUEUES_KEY = 'queues';
28
    public const CONTAINER_KEY = 'container';
29
    public const CONTEXTS_KEY = 'contexts';
30
    public const CONTEXTS_TYPE_KEY = 'context_type';
31
    public const DEFAULT_CONTEXT_TYPE = 'null';
32
    public const DEFAULT_CONTEXT_KEY = 'default_context';
33
    public const CONTEXT_SERVICE_KEY = 'context_service';
34
    public const DEFAULT_CONTEXT = 'default';
35
    public const DEFAULT_CONTEXT_SERVICE = 'queue.context.default';
36
    public const DEFAULT_CONTAINER_SERVICE = 'queue.container.default';
37
    public const MESSAGE_TYPES_KEY = 'message_types';
38
    public const INVALID_CONFIG_ARRAY_MESSAGE = 'Value for key "%s" must be of type array.';
39
    public const MISSING_CONFIG_MESSAGE = 'Missing config required key "%s", see the docs for complete config.';
40
    public const INVALID_CONTEXT_MESSAGE = 'Given context "%s" is not properly defined in the config.';
41
    public const DEFAULT_CONFIG = [
42
        self::QUEUES_KEY => [
43
            self::DEFAULT_CONTEXT_KEY => self::DEFAULT_CONTEXT,
44
            self::CONTEXTS_KEY => [
45
                self::DEFAULT_CONTEXT => [
46
                    self::CONTEXTS_TYPE_KEY => self::DEFAULT_CONTEXT_TYPE,
47
                    self::CONTEXT_SERVICE_KEY => self::DEFAULT_CONTEXT_SERVICE,
48
                    self::CONTAINER_KEY => self::DEFAULT_CONTAINER_SERVICE,
49
                ],
50
            ],
51
        ],
52
        'factories' => [
53
            self::DEFAULT_CONTEXT . '.action.container' => ActionContainerFactory::class,
54
            MessageProcessor::class => MessageProcessorFactory::class,
55
            Producer::class => ProducerFactory::class,
56
        ],
57
        'services' => [
58
            'null.context' => NullContext::class,
59
        ],
60
        'console' => [
61
            'commands' => [
62
                StartQueueConsumer::NAME => StartQueueConsumer::class,
63
            ],
64
            'factories' => [
65
                StartQueueConsumer::class => StartQueueConsumerFactory::class,
66
            ],
67
            'services' => [
68
                QueueConsumerInterface::class => QueueConsumer::class,
69
            ]
70
        ],
71
    ];
72
73 1
    public function __invoke(): array
74
    {
75 1
        return self::DEFAULT_CONFIG;
76
    }
77
78 4
    public static function getContextConfig(string $contextName, array $config): array
79
    {
80 4
        ConfigProvider::validate($config);
81
82 4
        $contextsConfig = $config[ConfigProvider::QUEUES_KEY][ConfigProvider::CONTEXTS_KEY];
83 4
        if (false === array_key_exists($contextName, $contextsConfig)) {
84 1
            throw new InvalidArgumentException(sprintf(ConfigProvider::INVALID_CONTEXT_MESSAGE, $contextName));
85
        }
86
87 3
        return $contextsConfig[$contextName];
88
    }
89
90
91
    /**
92
     * @param array $config
93
     * @throws AssertionFailedException
94
     */
95 4
    public static function validate(array $config): void
96
    {
97 4
        Assertion::keyExists($config, self::QUEUES_KEY, sprintf(self::MISSING_CONFIG_MESSAGE, self::QUEUES_KEY));
98 4
        Assertion::isArray($config[self::QUEUES_KEY], sprintf(self::INVALID_CONFIG_ARRAY_MESSAGE, self::QUEUES_KEY));
99 4
        Assertion::keyExists(
100 4
            $config[self::QUEUES_KEY],
101 4
            self::CONTEXTS_KEY,
102 4
            sprintf(self::MISSING_CONFIG_MESSAGE, self::CONTEXTS_KEY)
103
        );
104 4
    }
105
}
106