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
|
|||
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 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: