ActionContainer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A has() 0 3 1
A get() 0 13 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