ActionContainer::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Queue;
6
7
use Antidot\Queue\Exception\ActionContainerException;
8
use Antidot\Queue\Exception\ActionNotFoundException;
9
use Psr\Container\ContainerInterface;
10
use Throwable;
11
use function array_key_exists;
12
13
class ActionContainer implements ContainerInterface
14
{
15
    /** @var array<callable>  */
16
    private array $actions;
17
18
    /**
19
     * @param array<callable> $actions
20
     */
21 4
    public function __construct(array $actions)
22
    {
23 4
        $this->actions = $actions;
24 4
    }
25
26 3
    public function get($id): callable
27
    {
28 3
        if (false === $this->has($id)) {
29 1
            throw ActionNotFoundException::withId($id);
30
        }
31
32
        try {
33 2
            $action = $this->actions[$id]();
34 1
        } catch (Throwable $exception) {
35 1
            throw ActionContainerException::withIdAndPreviousException($id, $exception);
36
        }
37
38 1
        return $action;
39
    }
40
41 3
    public function has($id): bool
42
    {
43 3
        return array_key_exists($id, $this->actions);
44
    }
45
}
46