AdapterFactory::getAdapter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 16
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Adapter;
4
5
use GuzzleHttp\Client;
6
7
class AdapterFactory
8
{
9
    /** @var array<string, string> */
10
    private static array $adapters = [
11
        GistAdapter::TYPE => GistAdapter::class,
12
        YamlAdapter::TYPE => YamlAdapter::class
13
    ];
14
15
    public static function getAdapter(string $adapterType, array $config, Client $client): Adapter
16
    {
17
        if (!array_key_exists($adapterType, self::$adapters)) {
18
            throw new \RuntimeException("The adapter type $adapterType is not know. Allowed types are " . implode(', ', array_keys(self::$adapters)) . '.');
19
        }
20
21
        $adapterClass = self::$adapters[$adapterType];
22
23
        /** @var Adapter $adapter */
24
        $adapter = call_user_func([$adapterClass, 'fromConfigArray'], $config, $client);
25
26
        if ($adapter instanceof ClientAwareAdapter) {
27
            $adapter->setClient($client);
28
        }
29
30
        return $adapter;
31
    }
32
}
33