22
33namespace JustBetter \MagentoClient \Client ;
44
5+ use Closure ;
56use Generator ;
67use Illuminate \Http \Client \PendingRequest ;
78use Illuminate \Http \Client \Response ;
@@ -16,6 +17,8 @@ class Magento
1617
1718 public ?string $ storeCode = null ;
1819
20+ public ?Closure $ interceptor ;
21+
1922 public function __construct (
2023 protected BuildsRequest $ request
2124 ) {
@@ -42,7 +45,7 @@ public function graphql(string $query, array $variables = []): Response
4245 $ endpoint = config ("magento.connections. {$ this ->connection }.graphql_path " );
4346
4447 /** @var Response $response */
45- $ response = $ this ->request -> build ( $ this -> connection )
48+ $ response = $ this ->request ( )
4649 ->when ($ this ->storeCode !== null , fn (PendingRequest $ request ): PendingRequest => $ request ->withHeaders (['Store ' => $ this ->storeCode ]))
4750 ->post ($ endpoint , [
4851 'query ' => $ query ,
@@ -54,104 +57,103 @@ public function graphql(string $query, array $variables = []): Response
5457
5558 public function get (string $ path , array $ data = []): Response
5659 {
57- /** @var Response $response */
58- $ response = $ this ->request ->build ($ this ->connection )->get ($ this ->getUrl ($ path ), $ data );
60+ $ response = $ this ->request ()->get ($ this ->getUrl ($ path ), $ data );
5961
6062 return $ response ;
6163 }
6264
6365 public function post (string $ path , array $ data = []): Response
6466 {
6567 /** @var Response $response */
66- $ response = $ this ->request -> build ( $ this -> connection )->post ($ this ->getUrl ($ path ), $ data );
68+ $ response = $ this ->request ( )->post ($ this ->getUrl ($ path ), $ data );
6769
6870 return $ response ;
6971 }
7072
7173 public function postAsync (string $ path , array $ data = []): Response
7274 {
7375 /** @var Response $response */
74- $ response = $ this ->request -> build ( $ this -> connection )->post ($ this ->getUrl ($ path , true ), $ data );
76+ $ response = $ this ->request ( )->post ($ this ->getUrl ($ path , true ), $ data );
7577
7678 return $ response ;
7779 }
7880
7981 public function postBulk (string $ path , array $ data = []): Response
8082 {
8183 /** @var Response $response */
82- $ response = $ this ->request -> build ( $ this -> connection )->post ($ this ->getUrl ($ path , true , true ), $ data );
84+ $ response = $ this ->request ( )->post ($ this ->getUrl ($ path , true , true ), $ data );
8385
8486 return $ response ;
8587 }
8688
8789 public function patch (string $ path , array $ data = []): Response
8890 {
8991 /** @var Response $response */
90- $ response = $ this ->request -> build ( $ this -> connection )->patch ($ this ->getUrl ($ path ), $ data );
92+ $ response = $ this ->request ( )->patch ($ this ->getUrl ($ path ), $ data );
9193
9294 return $ response ;
9395 }
9496
9597 public function patchAsync (string $ path , array $ data = []): Response
9698 {
9799 /** @var Response $response */
98- $ response = $ this ->request -> build ( $ this -> connection )->patch ($ this ->getUrl ($ path , true ), $ data );
100+ $ response = $ this ->request ( )->patch ($ this ->getUrl ($ path , true ), $ data );
99101
100102 return $ response ;
101103 }
102104
103105 public function patchBulk (string $ path , array $ data = []): Response
104106 {
105107 /** @var Response $response */
106- $ response = $ this ->request -> build ( $ this -> connection )->patch ($ this ->getUrl ($ path , true , true ), $ data );
108+ $ response = $ this ->request ( )->patch ($ this ->getUrl ($ path , true , true ), $ data );
107109
108110 return $ response ;
109111 }
110112
111113 public function put (string $ path , array $ data = []): Response
112114 {
113115 /** @var Response $response */
114- $ response = $ this ->request -> build ( $ this -> connection )->put ($ this ->getUrl ($ path ), $ data );
116+ $ response = $ this ->request ( )->put ($ this ->getUrl ($ path ), $ data );
115117
116118 return $ response ;
117119 }
118120
119121 public function putAsync (string $ path , array $ data = []): Response
120122 {
121123 /** @var Response $response */
122- $ response = $ this ->request -> build ( $ this -> connection )->put ($ this ->getUrl ($ path , true ), $ data );
124+ $ response = $ this ->request ( )->put ($ this ->getUrl ($ path , true ), $ data );
123125
124126 return $ response ;
125127 }
126128
127129 public function putBulk (string $ path , array $ data = []): Response
128130 {
129131 /** @var Response $response */
130- $ response = $ this ->request -> build ( $ this -> connection )->put ($ this ->getUrl ($ path , true , true ), $ data );
132+ $ response = $ this ->request ( )->put ($ this ->getUrl ($ path , true , true ), $ data );
131133
132134 return $ response ;
133135 }
134136
135137 public function delete (string $ path , array $ data = []): Response
136138 {
137139 /** @var Response $response */
138- $ response = $ this ->request -> build ( $ this -> connection )->delete ($ this ->getUrl ($ path ), $ data );
140+ $ response = $ this ->request ( )->delete ($ this ->getUrl ($ path ), $ data );
139141
140142 return $ response ;
141143 }
142144
143145 public function deleteAsync (string $ path , array $ data = []): Response
144146 {
145147 /** @var Response $response */
146- $ response = $ this ->request -> build ( $ this -> connection )->delete ($ this ->getUrl ($ path , true ), $ data );
148+ $ response = $ this ->request ( )->delete ($ this ->getUrl ($ path , true ), $ data );
147149
148150 return $ response ;
149151 }
150152
151153 public function deleteBulk (string $ path , array $ data = []): Response
152154 {
153155 /** @var Response $response */
154- $ response = $ this ->request -> build ( $ this -> connection )->delete ($ this ->getUrl ($ path , true , true ), $ data );
156+ $ response = $ this ->request ( )->delete ($ this ->getUrl ($ path , true , true ), $ data );
155157
156158 return $ response ;
157159 }
@@ -206,6 +208,25 @@ public function getUrl(string $path, bool $async = false, bool $bulk = false): s
206208 return implode ('/ ' , $ options );
207209 }
208210
211+ public function intercept (Closure $ callable ): static
212+ {
213+ $ this ->interceptor = $ callable ;
214+
215+ return $ this ;
216+ }
217+
218+ protected function request (): PendingRequest
219+ {
220+ $ request = $ this ->request ->build ($ this ->connection );
221+
222+ if (isset ($ this ->interceptor )) {
223+ call_user_func ($ this ->interceptor , $ request );
224+ $ this ->interceptor = null ;
225+ }
226+
227+ return $ request ;
228+ }
229+
209230 public static function fake (): void
210231 {
211232 config ()->set ('magento.connection ' , 'default ' );
0 commit comments