Passed
Push — master ( f59089...907e2e )
by Julien
01:19 queued 12s
created

SdkClientRegistry::getSdkClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk;
6
7
use Mapado\RestClientSdk\Exception\SdkClientNotFoundException;
8
9
class SdkClientRegistry
10
{
11
    /** var array<string, SdkClient> $sdkClientList */
12
    private $sdkClientList;
13
14
    /**
15
     * @param array<string, SdkClient> $sdkClientList
16
     */
17
    public function __construct(array $sdkClientList)
18
    {
19 1
        $this->sdkClientList = $sdkClientList;
20 1
    }
21
22
    public function getSdkClient(string $name): SdkClient
23
    {
24 1
        $client = $this->sdkClientList[$name] ?? null;
25
26 1
        if (!$client) {
27 1
            throw new SdkClientNotFoundException(
28 1
                'Sdk client not found for name ' . $name
29
            );
30
        }
31
32 1
        return $client;
33
    }
34
35
    /**
36
     * @return array<SdkClient>
37
     */
38
    public function getSdkClientList(): array
39
    {
40 1
        return $this->sdkClientList;
41
    }
42
43
    public function getSdkClientForClass(string $entityClassname): SdkClient
44
    {
45 1
        foreach ($this->sdkClientList as $sdkClient) {
46 1
            if ($sdkClient->getMapping()->hasClassMetadata($entityClassname)) {
47 1
                return $sdkClient;
48
            }
49
        }
50
51 1
        throw new SdkClientNotFoundException(
52 1
            'Sdk client not found for entity class ' . $entityClassname
53
        );
54
    }
55
}
56