ClientBuilder::createClientV3()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDecool\Clubhouse;
6
7
use Http\{
8
    Client\Common\HttpMethodsClient,
9
    Client\Common\Plugin\AddHostPlugin,
10
    Client\Common\Plugin\AddPathPlugin,
11
    Client\Common\Plugin\AuthenticationPlugin,
12
    Client\Common\Plugin\HeaderSetPlugin,
13
    Client\Common\PluginClient,
14
    Client\HttpClient,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, JDecool\Clubhouse\HttpClient. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
    Discovery\Psr17FactoryDiscovery,
16
    Discovery\Psr18ClientDiscovery,
17
    Message\Authentication\QueryParam,
18
    Message\RequestFactory,
19
};
20
use JDecool\Clubhouse\Exception\ClubhouseException;
21
use Psr\Http\Message\UriFactoryInterface;
22
23
class ClientBuilder
24
{
25
    private const ENDPOINT_V1 = 'https://api.clubhouse.io/api/v1';
26
    private const ENDPOINT_V2 = 'https://api.clubhouse.io/api/v2';
27
    private const ENDPOINT_V3 = 'https://api.clubhouse.io/api/v3';
28
    private const ENDPOINT_BETA = 'https://api.clubhouse.io/api/beta';
29
30
    private $httpClient;
31
    private $requestFactory;
32
    private $uriFactory;
33
34
    public function __construct(
35
        ?HttpClient $httpClient = null,
36
        ?RequestFactory $requestFactory = null,
37
        ?UriFactoryInterface $uriFactory = null
38
    ) {
39
        $this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
40
        $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
41
        $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory();
42
    }
43
44
    public function createClientV1(string $token): Client
45
    {
46
        return $this->create(self::ENDPOINT_V1, $token);
47
    }
48
49
    public function createClientV2(string $token): Client
50
    {
51
        return $this->create(self::ENDPOINT_V2, $token);
52
    }
53
54
    public function createClientV3(string $token): Client
55
    {
56
        return $this->create(self::ENDPOINT_V3, $token);
57
    }
58
59
    public function createClientBeta(string $token): Client
60
    {
61
        return $this->create(self::ENDPOINT_BETA, $token);
62
    }
63
64
    public function create(string $url, string $token): Client
65
    {
66
        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
67
            throw new ClubhouseException('Invalid Clubouse base URI.');
68
        }
69
70
        if ('' === trim($token)) {
71
            throw new ClubhouseException('API token is required.');
72
        }
73
74
        $plugins = [
75
            new AuthenticationPlugin(
76
                new QueryParam(['token' => $token])
77
            ),
78
            new AddHostPlugin(
79
                $this->uriFactory->createUri($url)
80
            ),
81
            new AddPathPlugin(
82
                $this->uriFactory->createUri($url)
83
            ),
84
            new HeaderSetPlugin([
85
                'User-Agent' => 'github.com/jdecool/clubhouse-api',
86
                'Content-Type' => 'application/json',
87
            ])
88
        ];
89
90
        $http = new HttpMethodsClient(
91
            new PluginClient($this->httpClient, $plugins),
92
            $this->requestFactory
93
        );
94
95
        return new Client($http);
96
    }
97
}
98