From 26245bb30a2fe3ae203e8abf70a856a40a551960 Mon Sep 17 00:00:00 2001 From: Dimitris Bozelos <dbozelos@gmail.com> Date: Thu, 13 Jul 2023 03:34:12 +0000 Subject: [PATCH] Issue #3374278 Made it easy to override guzzle client creation --- src/EntitySync/Api/ObjectClient.php | 61 ++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/EntitySync/Api/ObjectClient.php b/src/EntitySync/Api/ObjectClient.php index f34aa61..7208d25 100644 --- a/src/EntitySync/Api/ObjectClient.php +++ b/src/EntitySync/Api/ObjectClient.php @@ -60,13 +60,16 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { ]); } - // phpcs:disable - // @I Allow injecting the Guzzle client for mocking or config purposes - // type : task - // priority : low - // labels : testing - // phpcs:enable - $discovery = new Discovery(new Client(), $this->sessionManager); + $discovery = new Discovery( + $this->getGuzzleClient( + __METHOD__, + [ + 'filters' => $filters, + 'options' => $options, + ] + ), + $this->sessionManager + ); $api = lcfirst($this->objectType); $objects = $discovery ->{$api}() @@ -158,7 +161,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { $object = new $class($fields); $discovery = new Discovery( - $this->getGuzzleClient($branch), + $this->getGuzzleClient( + __METHOD__, + [ + 'branch' => $branch, + 'options' => $options, + ] + ), $this->sessionManager ); $api = lcfirst($this->objectType); @@ -209,7 +218,13 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { $object->setId($id); $discovery = new Discovery( - $this->getGuzzleClient($branch), + $this->getGuzzleClient( + __METHOD__, + [ + 'branch' => $branch, + 'options' => $options, + ] + ), $this->sessionManager ); $api = lcfirst($this->objectType); @@ -530,20 +545,32 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { * * Currently, it adds the appropriate header if a branch is given. * - * @param string|null $branch - * The Acumatica branch to use for the header, or NULL to not include the - * branch header. + * @param string $caller + * The name of the method that requested the client e.g. `create`, `update` + * etc. + * @param array $options + * An array of options that may be used to determine the middleware that + * will be added to the client handler. This is not the same as, but may + * contain, the client options passed to the caller method. Supported + * options by the default implementation are: + * - branch (string, optional): + * The Acumatica branch to use for the header, or NULL to not include the + * branch header. * * @return \GuzzleHttp\Client * The instantiated Guzzle client. */ - protected function getGuzzleClient(string|null $branch): Client { - if ($branch === NULL) { + protected function getGuzzleClient( + string $caller, + array $options = [] + ): Client { + if (!isset($options['branch'])) { return new Client(); } // We use a middleware so that all requests sent by this client will have // the header. + $branch = $options['branch']; $handler = HandlerStack::create(); $handler->push(Middleware::mapRequest( function (RequestInterface $request) use ($branch) { @@ -551,6 +578,12 @@ class ObjectClient implements ClientInterface, ObjectClientInterface { }) ); + // phpcs:disable + // @I Allow injecting the Guzzle client for mocking or config purposes + // type : task + // priority : low + // labels : testing + // phpcs:enable return new Client(['handler' => $handler]); } -- GitLab