2
2
3
3
namespace ApiClients \Client \Twitter ;
4
4
5
+ use ApiClients \Client \Twitter \Resource \Async \Profile ;
5
6
use ApiClients \Client \Twitter \Resource \ProfileInterface ;
6
7
use ApiClients \Client \Twitter \Resource \TweetInterface ;
7
8
use ApiClients \Client \Twitter \Resource \UserInterface ;
9
+ use ApiClients \Foundation \Client as FoundationClient ;
10
+ use ApiClients \Foundation \Factory ;
11
+ use ApiClients \Foundation \Hydrator \CommandBus \Command \BuildSyncFromAsyncCommand ;
12
+ use ApiClients \Foundation \Oauth1 \Middleware \Oauth1Middleware ;
13
+ use ApiClients \Foundation \Oauth1 \Options as Oauth1Options ;
14
+ use ApiClients \Foundation \Options ;
15
+ use ApiClients \Foundation \Transport \Options as TransportOptions ;
16
+ use ApiClients \Tools \Psr7 \Oauth1 \Definition ;
8
17
use React \EventLoop \Factory as LoopFactory ;
9
18
use React \EventLoop \LoopInterface ;
10
19
use function Clue \React \Block \await ;
11
20
12
21
final class Client implements ClientInterface
13
22
{
23
+ /**
24
+ * @var string
25
+ */
26
+ private $ consumerKey ;
27
+
28
+ /**
29
+ * @var string
30
+ */
31
+ private $ consumerSecret ;
32
+
14
33
/**
15
34
* @var LoopInterface
16
35
*/
@@ -19,32 +38,83 @@ final class Client implements ClientInterface
19
38
/**
20
39
* @var AsyncClient
21
40
*/
41
+ protected $ asyncClient ;
42
+
43
+ /**
44
+ * @var FoundationClient
45
+ */
22
46
protected $ client ;
23
47
24
48
/**
25
49
* @var StreamingClient
26
50
*/
27
51
protected $ streamingClient ;
28
52
53
+ /**
54
+ * @var array
55
+ */
56
+ protected $ options ;
57
+
29
58
public function __construct (
30
59
string $ consumerKey ,
31
60
string $ consumerSecret
32
61
) {
62
+ $ this ->consumerKey = $ consumerKey ;
63
+ $ this ->consumerSecret = $ consumerSecret ;
33
64
$ this ->loop = LoopFactory::create ();
34
- $ this ->client = new AsyncClient ($ consumerKey , $ consumerSecret , $ this ->loop );
65
+
66
+ $ this ->options = ApiSettings::getOptions (
67
+ $ consumerKey ,
68
+ $ consumerSecret ,
69
+ 'Sync '
70
+ );
71
+
72
+ $ this ->client = Factory::create ($ this ->loop , $ this ->options );
73
+
74
+ $ this ->asyncClient = new AsyncClient ($ consumerKey , $ consumerSecret , $ this ->loop , [], $ this ->client );
35
75
}
36
76
37
77
public function withAccessToken (string $ accessToken , string $ accessTokenSecret ): Client
38
78
{
79
+ $ options = $ this ->options ;
80
+ // @codingStandardsIgnoreStart
81
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ] = new Definition \AccessToken ($ accessToken );
82
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ] = new Definition \TokenSecret ($ accessTokenSecret );
83
+ // @codingStandardsIgnoreEnd
84
+
39
85
$ clone = clone $ this ;
40
- $ clone ->client = $ this ->client ->withAccessToken ($ accessToken , $ accessTokenSecret );
86
+ $ clone ->client = Factory::create ($ this ->loop , $ options );
87
+ $ clone ->asyncClient = (new AsyncClient (
88
+ $ this ->consumerKey ,
89
+ $ this ->consumerSecret ,
90
+ $ this ->loop ,
91
+ [],
92
+ $ this ->client
93
+ ))->withAccessToken ($ accessToken , $ accessTokenSecret );
41
94
return $ clone ;
42
95
}
43
96
44
97
public function withOutAccessToken (): Client
45
98
{
99
+ $ options = $ this ->options ;
100
+ // @codingStandardsIgnoreStart
101
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ])) {
102
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ]);
103
+ }
104
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ])) {
105
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ]);
106
+ }
107
+ // @codingStandardsIgnoreEnd
108
+
46
109
$ clone = clone $ this ;
47
- $ clone ->client = $ this ->client ->withOutAccessToken ();
110
+ $ clone ->client = Factory::create ($ this ->loop , $ options );
111
+ $ clone ->asyncClient = (new AsyncClient (
112
+ $ this ->consumerKey ,
113
+ $ this ->consumerSecret ,
114
+ $ this ->loop ,
115
+ [],
116
+ $ this ->client
117
+ ))->withOutAccessToken ();
48
118
return $ clone ;
49
119
}
50
120
@@ -53,8 +123,8 @@ public function stream(): StreamingClient
53
123
if (!($ this ->streamingClient instanceof StreamingClient)) {
54
124
$ this ->streamingClient = new StreamingClient (
55
125
$ this ->loop ,
56
- $ this ->client ->getCommandBus (),
57
- $ this ->client ->stream ()
126
+ $ this ->asyncClient ->getCommandBus (),
127
+ $ this ->asyncClient ->stream ()
58
128
);
59
129
}
60
130
@@ -64,23 +134,25 @@ public function stream(): StreamingClient
64
134
public function tweet (string $ tweet ): TweetInterface
65
135
{
66
136
return await (
67
- $ this ->client ->tweet ($ tweet ),
137
+ $ this ->asyncClient ->tweet ($ tweet ),
68
138
$ this ->loop
69
139
);
70
140
}
71
141
72
142
public function profile (): ProfileInterface
73
143
{
74
144
return await (
75
- $ this ->client ->profile (),
145
+ $ this ->asyncClient ->profile ()->then (function (Profile $ profile ) {
146
+ return $ this ->client ->handle (new BuildSyncFromAsyncCommand (ProfileInterface::HYDRATE_CLASS , $ profile ));
147
+ }),
76
148
$ this ->loop
77
149
);
78
150
}
79
151
80
152
public function user (string $ tweet ): UserInterface
81
153
{
82
154
return await (
83
- $ this ->client ->user ($ tweet ),
155
+ $ this ->asyncClient ->user ($ tweet ),
84
156
$ this ->loop
85
157
);
86
158
}
0 commit comments