Psr11ContainerCallbackResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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