Skip to content

Commit

Permalink
Updating Bearer class wrapper to instantiate it through object manage…
Browse files Browse the repository at this point in the history
…r to have DI power and more custom classes
  • Loading branch information
MeCapron committed Oct 24, 2023
1 parent 9096537 commit 8ff216d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
16 changes: 0 additions & 16 deletions Exceptions/InvalidBearerTokenException.php

This file was deleted.

30 changes: 18 additions & 12 deletions Http/Plugins/Auth/BearerPluginWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,37 @@

namespace Adexos\JaneSDKBridge\Http\Plugins\Auth;

use Adexos\JaneSDKBridge\Exceptions\InvalidBearerTokenException;
use Http\Client\Common\Plugin;
use Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\ObjectManagerInterface;
use RuntimeException;

use function get_class;

class BearerPluginWrapper implements AuthHttpPluginInterface
{
private ObjectManagerInterface $objectManager;
private string $bearerPluginClass;

public function __construct(string $bearerPluginClass)
public function __construct(ObjectManagerInterface $objectManager, string $bearerPluginClass)
{
$this->objectManager = $objectManager;
$this->bearerPluginClass = $bearerPluginClass;
}

/**
* @throws InvalidBearerTokenException
*/
public function create(array $options = []): AuthenticationPlugin
{
if (!isset($options['Bearer'])) {
throw new InvalidBearerTokenException();
$object = $this->objectManager->create($this->bearerPluginClass, $options);

if (!$object instanceof AuthenticationPlugin) {
throw new RuntimeException(
sprintf(
'%s must implements %s',
get_class($object),
AuthenticationPlugin::class
)
);
}

return new $this->bearerPluginClass($options['Bearer']);
return $object;
}
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# How to install

You can install it by typing : `composer require adexos/m2-jane-sdk-bridge:^1.0`
You can install it by typing : `composer require adexos/m2-jane-sdk-bridge`

# How to use

Expand Down Expand Up @@ -71,9 +71,8 @@ class MyClient
{
//This class allows you to have an attachment point to the HTTP client
//and to have hint types because of the generic SDKs
//you can do whatever you want here such as doe somes checks or passing Bearer token :
// $this->tokenStrategy->getToken();
// return $this->client->getClient(['Bearer' => $token]);
//you can do whatever you want here such as do some checks or passing scope for the bearer token :
// return $this->client->getClient(['scope' => 'my_custom_scope'']);

return $this->client->getClient();
}
Expand Down

0 comments on commit 8ff216d

Please sign in to comment.