Invokable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 38
ccs 10
cts 14
cp 0.7143
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 2
A __construct() 0 1 1
A create() 0 24 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary\Factory;
6
7
use InvalidArgumentException;
8
use Knp\DictionaryBundle\Dictionary;
9
use Knp\DictionaryBundle\Dictionary\Factory;
10
use Psr\Container\ContainerInterface;
11
12
final class Invokable implements Factory
13
{
14
    public function __construct(private readonly ContainerInterface $container) {}
15
16
    /**
17
     * {@inheritdoc}
18
     *
19 4
     * @throw InvalidArgumentException if there is some problem with the config.
20
     */
21 4
    public function create(string $name, array $config): Dictionary
22 4
    {
23
        if (!isset($config['service'])) {
24
            throw new \InvalidArgumentException(\sprintf(
25
                'The "service" config key must be set for the dictionary named "%s".',
26
                $name
27
            ));
28
        }
29 1
30
        $service  = $this->container->get($config['service']);
31 1
        $callable = [$service];
32
33
        if (isset($config['method'])) {
34
            $callable[] = $config['method'];
35
        }
36
37
        if (!\is_callable($callable)) {
38 1
            throw new \InvalidArgumentException(\sprintf(
39 1
                'You must provide a valid callable for the dictionary named "%s".',
40
                $name
41 1
            ));
42 1
        }
43
44
        return new Dictionary\Invokable($name, $callable);
45 1
    }
46
47
    public function supports(array $config): bool
48
    {
49
        return isset($config['type']) && 'callable' === $config['type'];
50
    }
51
}
52