Psr11ContainerCallbackResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Tarantool\JobQueue\Executor\CallbackResolver;
4
5
use Psr\Container\ContainerInterface as Container;
6
use Tarantool\JobQueue\Exception\BadPayloadException;
7
use Tarantool\JobQueue\JobBuilder\JobOptions;
8
9
class Psr11ContainerCallbackResolver implements CallbackResolver
10
{
11
    private $container;
12
    private $idFormat;
13
14 6
    public function __construct(Container $container, string $idFormat = '%s')
15
    {
16 6
        $this->container = $container;
17 6
        $this->idFormat = $idFormat;
18 6
    }
19
20 6
    public function resolve($payload): callable
21
    {
22 6
        if (empty($payload[JobOptions::PAYLOAD_SERVICE_ID])) {
23 1
            throw BadPayloadException::missingOrEmptyKeyValue($payload, JobOptions::PAYLOAD_SERVICE_ID, 'string', __CLASS__);
24
        }
25
26 5
        $id = sprintf($this->idFormat, $payload[JobOptions::PAYLOAD_SERVICE_ID]);
27
28 5
        return empty($payload[JobOptions::PAYLOAD_SERVICE_METHOD])
29
            ? $this->container->get($id)
30
            : [$this->container->get($id), $payload[JobOptions::PAYLOAD_SERVICE_METHOD]];
31
    }
32
}
33