1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pnz\MattermostClient; |
6
|
|
|
|
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Http\Discovery\HttpClientDiscovery; |
9
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
10
|
|
|
use Http\Message\MessageFactory; |
11
|
|
|
use Http\Message\RequestFactory; |
12
|
|
|
use Pnz\MattermostClient\Api\Channels; |
13
|
|
|
use Pnz\MattermostClient\Api\Files; |
14
|
|
|
use Pnz\MattermostClient\Api\Posts; |
15
|
|
|
use Pnz\MattermostClient\Api\Teams; |
16
|
|
|
use Pnz\MattermostClient\Api\Users; |
17
|
|
|
use Pnz\MattermostClient\Hydrator\Hydrator; |
18
|
|
|
use Pnz\MattermostClient\Hydrator\ModelHydrator; |
19
|
|
|
|
20
|
|
|
final class ApiClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HttpClient |
24
|
|
|
*/ |
25
|
|
|
private $httpClient; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Hydrator |
29
|
|
|
*/ |
30
|
|
|
private $hydrator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RequestFactory |
34
|
|
|
*/ |
35
|
|
|
private $messageFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Construct an ApiClient instance. |
39
|
|
|
* |
40
|
|
|
* @param HttpClient|null $httpClient The HttpClient; if null, the auto-discover will be used |
41
|
|
|
* @param MessageFactory|null $requestFactory The MessageFactory; if null, the auto-discover will be used |
42
|
|
|
* @param Hydrator|null $hydrator The Hydrator to use, default to the ModelHydrator |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
HttpClient $httpClient = null, |
46
|
|
|
MessageFactory $requestFactory = null, |
47
|
|
|
Hydrator $hydrator = null |
48
|
|
|
) { |
49
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
50
|
|
|
$this->messageFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
51
|
|
|
$this->hydrator = $hydrator ?: new ModelHydrator(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns configured ApiClient from the given Configurator, Hydrator and RequestFactory. |
56
|
|
|
* |
57
|
|
|
* @param HttpClientConfigurator $httpClientConfigurator |
58
|
|
|
* @param Hydrator|null $hydrator |
59
|
|
|
* @param RequestFactory|null $requestFactory |
60
|
|
|
* |
61
|
|
|
* @return ApiClient |
62
|
|
|
*/ |
63
|
|
|
public static function configure( |
64
|
|
|
HttpClientConfigurator $httpClientConfigurator, |
65
|
|
|
RequestFactory $requestFactory = null, |
66
|
|
|
Hydrator $hydrator = null |
67
|
|
|
): self { |
68
|
|
|
$httpClient = $httpClientConfigurator->createConfiguredClient(); |
69
|
|
|
|
70
|
|
|
return new self($httpClient, $requestFactory, $hydrator); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return a client handling the Users resources. |
75
|
|
|
* |
76
|
|
|
* @return Api\Users |
77
|
|
|
*/ |
78
|
|
|
public function users(): Users |
79
|
|
|
{ |
80
|
|
|
return new Api\Users($this->httpClient, $this->messageFactory, $this->hydrator); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return a client handling the Teams resources. |
85
|
|
|
* |
86
|
|
|
* @return Api\Teams |
87
|
|
|
*/ |
88
|
|
|
public function teams(): Teams |
89
|
|
|
{ |
90
|
|
|
return new Api\Teams($this->httpClient, $this->messageFactory, $this->hydrator); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return a client handling the Channels resources. |
95
|
|
|
* |
96
|
|
|
* @return Api\Channels |
97
|
|
|
*/ |
98
|
|
|
public function channels(): Channels |
99
|
|
|
{ |
100
|
|
|
return new Api\Channels($this->httpClient, $this->messageFactory, $this->hydrator); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return a client handling the Posts resources. |
105
|
|
|
* |
106
|
|
|
* @return Api\Posts |
107
|
|
|
*/ |
108
|
|
|
public function posts(): Posts |
109
|
|
|
{ |
110
|
|
|
return new Api\Posts($this->httpClient, $this->messageFactory, $this->hydrator); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return a client handling the Files resources. |
116
|
|
|
* |
117
|
|
|
* @return Api\Files |
118
|
|
|
*/ |
119
|
|
|
public function files(): Files |
120
|
|
|
{ |
121
|
|
|
return new Api\Files($this->httpClient, $this->messageFactory, $this->hydrator); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.