Completed
Push — master ( 4b6f39...7d423b )
by Jérémy
12s queued 11s
created

ClientBuilder::createClientBeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDecool\Clubhouse;
6
7
use Http\{
8
    Client\Common\HttpMethodsClient,
9
    Client\HttpClient,
10
    Discovery\HttpClientDiscovery,
11
    Discovery\MessageFactoryDiscovery,
12
    Message\MessageFactory
13
};
14
use RuntimeException;
15
16
class ClientBuilder
17
{
18
    public const V1 = 'v1';
19
    public const V2 = 'v2';
20
    public const BETA = 'beta';
21
22
    private $httpClient;
23
    private $messageFactory;
24
25
    public function __construct(?HttpClient $httpClient = null, ?MessageFactory $messageFactory = null)
26
    {
27
        $this->httpClient = $httpClient ?? HttpClientDiscovery::find();
28
        $this->messageFactory = $messageFactory ?? MessageFactoryDiscovery::find();
29
    }
30
31
    public function createClientV1(string $token): Client
32
    {
33
        return $this->create(self::V1, $token);
34
    }
35
36
    public function createClientV2(string $token): Client
37
    {
38
        return $this->create(self::V2, $token);
39
    }
40
41
    public function createClientBeta(string $token): Client
42
    {
43
        return $this->create(self::BETA, $token);
44
    }
45
46
    public function create(string $version, string $token): Client
47
    {
48
        $http = new HttpMethodsClient($this->httpClient, $this->messageFactory);
49
50
        switch ($version) {
51
            case self::V1:
52
                return Client::createV1($http, $token);
53
54
            case self::V2:
55
                return Client::createV2($http, $token);
56
57
            case self::BETA:
58
                return Client::createBeta($http, $token);
59
        }
60
61
        throw new RuntimeException("Version '$version' is not supported.");
62
    }
63
}
64