You can try something like this (if you really see the value in the wrapper):
Class YourSoapApiClient
{
private $session_id;
public function __construct(private string $access_key); // set in your SP or use config() whatever you prefer
public function getJournal($someParams)
{
$this->init();
$this->soap->ProcessJournal([
‘SessionID’ => $this->session_id,
…$someParams,
]);
}
private function init()
{
If (!$this->initiated) {
$this->soap = new SoapClient(‘webservice_url’);
$authResponse = $this->soap->Authenticate([‘accessKey’ => $this->access_key]); // or config(‘your_api_key’)
if (!isset($authResponse->AuthenticateResult)) {
Throw new \Exception(‘cannot authenticate connection with YOUR_SOAP_SERVICE’)
}
$this->session_id = $authResponse->AuthenticateResult;
}
}
}
—
Extra:
I suggest you never put things like this in a ServiceProvider - I assume $this->requestAccessToken Is actually making API requests to an external service:
private function getAccessToken(): ?AccessToken
{
return $this->accessToken ?? $this->requestAccessToken($clientId, $clientSecret);
}
With this in your SP you put yourself at risk of breaking the whole application whenever this call fails (API is down, your code outdated, request fails for whatever reason). This would happen if you put the ExampleClient As a dependency in something else than a controller and unknowingly this class is being instantiated upon every call to your app.
Been there, done that - not recommended experience ;)