AuthenticationFactory::createPlugin()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 10
cc 2
nc 2
nop 1
crap 2.0185
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule\PluginFactory;
6
7
use Http\Client\Common\Plugin;
8
use Http\Client\Common\Plugin\AuthenticationPlugin;
9
use InvalidArgumentException;
10
11
class AuthenticationFactory implements PluginFactory
12
{
13
    /** @var array<string, Authentication\AuthenticationFactory> */
14
    private array $factories;
15
16
    /**
17
     * AuthenticationFactory constructor.
18
     *
19
     * @param array<string, Authentication\AuthenticationFactory> $factories
20
     */
21 1
    public function __construct(array $factories)
22
    {
23 1
        $this->factories = $factories;
24 1
    }
25
26
    /**
27
     * @param array<string, mixed> $config
28
     */
29
    public function createPlugin(array $config = []): Plugin
30
    {
31 1
        $options = $config['options'] ?? [];
32
        $type = $config['type'] ?? null;
33 1
34 1
        $authFactory = $this->factories[$type] ?? null;
35
36
        if (! $authFactory instanceof Authentication\AuthenticationFactory) {
37 1
            throw new InvalidArgumentException('Unsupported authentication type');
38
        }
39 1
40
        return new AuthenticationPlugin($authFactory->createAuthentication($options));
41
    }
42
}
43