2
2
3
3
namespace ApiClients \Client \Twitter ;
4
4
5
+ use ApiClients \Foundation \Client ;
5
6
use ApiClients \Foundation \Factory ;
6
7
use ApiClients \Foundation \Hydrator \CommandBus \Command \HydrateCommand ;
7
- use ApiClients \Foundation \Client ;
8
+ use ApiClients \Foundation \Oauth1 \Middleware \Oauth1Middleware ;
9
+ use ApiClients \Foundation \Oauth1 \Options as Oauth1Options ;
10
+ use ApiClients \Foundation \Options ;
8
11
use ApiClients \Foundation \Transport \CommandBus \Command \RequestCommand ;
9
12
use ApiClients \Foundation \Transport \CommandBus \Command \StreamingRequestCommand ;
13
+ use ApiClients \Foundation \Transport \Options as TransportOptions ;
14
+ use ApiClients \Tools \Psr7 \Oauth1 \Definition ;
10
15
use GuzzleHttp \Psr7 \Request ;
11
- use JacobKiers \OAuth \Consumer \Consumer ;
12
- use JacobKiers \OAuth \Token \Token ;
13
16
use Psr \Http \Message \RequestInterface ;
14
17
use Psr \Http \Message \ResponseInterface ;
15
18
use React \EventLoop \LoopInterface ;
16
19
use React \Promise \PromiseInterface ;
17
- use function React \Promise \resolve ;
18
20
use Rx \Extra \Operator \CutOperator ;
19
21
use Rx \Observable ;
20
22
use Rx \React \Promise ;
23
+ use function React \Promise \resolve ;
21
24
22
- class AsyncClient
25
+ final class AsyncClient
23
26
{
24
27
const STREAM_DELIMITER = "\r\n" ;
25
28
29
+ /**
30
+ * @var string
31
+ */
32
+ private $ consumerKey ;
33
+
34
+ /**
35
+ * @var string
36
+ */
37
+ private $ consumerSecret ;
38
+
39
+ /**
40
+ * @var LoopInterface
41
+ */
42
+ private $ loop ;
43
+
26
44
/**
27
45
* @var Client
28
46
*/
@@ -31,24 +49,64 @@ class AsyncClient
31
49
public function __construct (
32
50
string $ consumerKey ,
33
51
string $ consumerSecret ,
34
- string $ accessToken ,
35
- string $ accessTokenSecret ,
36
52
LoopInterface $ loop ,
53
+ array $ options = [],
37
54
Client $ client = null
38
55
) {
56
+ $ this ->consumerKey = $ consumerKey ;
57
+ $ this ->consumerSecret = $ consumerSecret ;
58
+ $ this ->loop = $ loop ;
59
+
39
60
if (!($ client instanceof Client)) {
40
61
$ this ->options = ApiSettings::getOptions (
41
62
$ consumerKey ,
42
63
$ consumerSecret ,
43
- $ accessToken ,
44
- $ accessTokenSecret ,
45
- 'Async '
64
+ 'Async ' ,
65
+ $ options
46
66
);
47
- $ client = Factory::create ($ loop , $ this ->options );
67
+
68
+ $ client = Factory::create ($ this ->loop , $ this ->options );
48
69
}
70
+
49
71
$ this ->client = $ client ;
50
72
}
51
73
74
+ public function withAccessToken (string $ accessToken , string $ accessTokenSecret ): AsyncClient
75
+ {
76
+ $ options = $ this ->options ;
77
+ // @codingStandardsIgnoreStart
78
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ] = new Definition \AccessToken ($ accessToken );
79
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ] = new Definition \TokenSecret ($ accessTokenSecret );
80
+ // @codingStandardsIgnoreEnd
81
+
82
+ return new self (
83
+ $ this ->consumerKey ,
84
+ $ this ->consumerSecret ,
85
+ $ this ->loop ,
86
+ $ options
87
+ );
88
+ }
89
+
90
+ public function withOutAccessToken (): AsyncClient
91
+ {
92
+ $ options = $ this ->options ;
93
+ // @codingStandardsIgnoreStart
94
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ])) {
95
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ]);
96
+ }
97
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ])) {
98
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ]);
99
+ }
100
+ // @codingStandardsIgnoreEnd
101
+
102
+ return new self (
103
+ $ this ->consumerKey ,
104
+ $ this ->consumerSecret ,
105
+ $ this ->loop ,
106
+ $ options
107
+ );
108
+ }
109
+
52
110
public function user (string $ user ): PromiseInterface
53
111
{
54
112
return $ this ->client ->handle (new RequestCommand (
0 commit comments