22
33namespace ApiClients \Client \Twitter ;
44
5+ use ApiClients \Foundation \Client ;
56use ApiClients \Foundation \Factory ;
67use 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 ;
811use ApiClients \Foundation \Transport \CommandBus \Command \RequestCommand ;
912use ApiClients \Foundation \Transport \CommandBus \Command \StreamingRequestCommand ;
13+ use ApiClients \Foundation \Transport \Options as TransportOptions ;
14+ use ApiClients \Tools \Psr7 \Oauth1 \Definition ;
1015use GuzzleHttp \Psr7 \Request ;
11- use JacobKiers \OAuth \Consumer \Consumer ;
12- use JacobKiers \OAuth \Token \Token ;
1316use Psr \Http \Message \RequestInterface ;
1417use Psr \Http \Message \ResponseInterface ;
1518use React \EventLoop \LoopInterface ;
1619use React \Promise \PromiseInterface ;
17- use function React \Promise \resolve ;
1820use Rx \Extra \Operator \CutOperator ;
1921use Rx \Observable ;
2022use Rx \React \Promise ;
23+ use function React \Promise \resolve ;
2124
22- class AsyncClient
25+ final class AsyncClient
2326{
2427 const STREAM_DELIMITER = "\r\n" ;
2528
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+
2644 /**
2745 * @var Client
2846 */
@@ -31,24 +49,64 @@ class AsyncClient
3149 public function __construct (
3250 string $ consumerKey ,
3351 string $ consumerSecret ,
34- string $ accessToken ,
35- string $ accessTokenSecret ,
3652 LoopInterface $ loop ,
53+ array $ options = [],
3754 Client $ client = null
3855 ) {
56+ $ this ->consumerKey = $ consumerKey ;
57+ $ this ->consumerSecret = $ consumerSecret ;
58+ $ this ->loop = $ loop ;
59+
3960 if (!($ client instanceof Client)) {
4061 $ this ->options = ApiSettings::getOptions (
4162 $ consumerKey ,
4263 $ consumerSecret ,
43- $ accessToken ,
44- $ accessTokenSecret ,
45- 'Async '
64+ 'Async ' ,
65+ $ options
4666 );
47- $ client = Factory::create ($ loop , $ this ->options );
67+
68+ $ client = Factory::create ($ this ->loop , $ this ->options );
4869 }
70+
4971 $ this ->client = $ client ;
5072 }
5173
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+
52110 public function user (string $ user ): PromiseInterface
53111 {
54112 return $ this ->client ->handle (new RequestCommand (
0 commit comments